(t *testing.T)
| 97 | } |
| 98 | |
| 99 | func 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 |
nothing calls this directly
no test coverage detected