MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestReleaseSequencesOnStop

Function TestReleaseSequencesOnStop

db/sequence_allocator_test.go:99–146  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

97}
98
99func TestReleaseSequencesOnStop(t *testing.T) {
100
101 ctx := base.TestCtx(t)
102 bucket := base.GetTestBucket(t)
103 defer bucket.Close(ctx)
104
105 sgw, err := base.NewSyncGatewayStats()
106 require.NoError(t, err)
107 dbstats, err := sgw.NewDBStats("", false, false, false, nil, nil)
108 require.NoError(t, err)
109 testStats := dbstats.Database()
110
111 oldFrequency := MaxSequenceIncrFrequency
112 defer func() { MaxSequenceIncrFrequency = oldFrequency }()
113 MaxSequenceIncrFrequency = 1000 * time.Millisecond
114 a, err := newSequenceAllocator(ctx, bucket.GetSingleDataStore(), testStats, base.DefaultMetadataKeys)
115 // Reduce sequence wait for Stop testing
116 a.releaseSequenceWait = 10 * time.Millisecond
117 assert.NoError(t, err, "error creating allocator")
118
119 // Initial allocation should use batch size of 1
120 nextSequence, err := a.nextSequence(ctx)
121 assert.NoError(t, err)
122 assert.Equal(t, uint64(1), nextSequence)
123 assertNewAllocatorStats(t, testStats, 1, 1, 1, 0, nextSequence, 1)
124
125 // Subsequent allocation should increase batch size to 2, allocate 1
126 nextSequence, err = a.nextSequence(ctx)
127 assert.NoError(t, err)
128 assert.Equal(t, uint64(2), nextSequence)
129 assertNewAllocatorStats(t, testStats, 2, 3, 2, 0, nextSequence, 3)
130
131 // Stop the allocator
132 a.Stop(ctx)
133
134 releasedCount := 0
135 // Ensure unused sequence is released on Stop
136 for i := 0; i < 20; i++ {
137 releasedCount = int(testStats.SequenceReleasedCount.Value())
138 if releasedCount == 1 {
139 break
140 }
141 time.Sleep(100 * time.Millisecond)
142 }
143 assert.Equal(t, 1, releasedCount, "Expected 1 released sequence")
144 assertNewAllocatorStats(t, testStats, 2, 3, 2, 1, nextSequence, 3)
145
146}
147
148// Reproduces deadlock from CBG-663. Required adding a sleep inside the <-time.After case in
149// releaseSequenceMonitor to reliably queue up a large number of sequence allocation requests

Callers

nothing calls this directly

Calls 13

TestCtxFunction · 0.92
GetTestBucketFunction · 0.92
NewSyncGatewayStatsFunction · 0.92
assertNewAllocatorStatsFunction · 0.85
NewDBStatsMethod · 0.80
nextSequenceMethod · 0.80
newSequenceAllocatorFunction · 0.70
CloseMethod · 0.65
StopMethod · 0.65
DatabaseMethod · 0.45
GetSingleDataStoreMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected