(t *testing.T)
| 27 | ) |
| 28 | |
| 29 | func TestSequenceAllocator(t *testing.T) { |
| 30 | |
| 31 | ctx := base.TestCtx(t) |
| 32 | bucket := base.GetTestBucket(t) |
| 33 | defer bucket.Close(ctx) |
| 34 | |
| 35 | sgw, err := base.NewSyncGatewayStats() |
| 36 | require.NoError(t, err) |
| 37 | dbstats, err := sgw.NewDBStats("", false, false, false, nil, nil) |
| 38 | require.NoError(t, err) |
| 39 | testStats := dbstats.Database() |
| 40 | |
| 41 | // Create a sequence allocator without using constructor, to test without a releaseSequenceMonitor |
| 42 | // - allows manually triggered release |
| 43 | a := &sequenceAllocator{ |
| 44 | datastore: bucket.GetSingleDataStore(), |
| 45 | dbStats: testStats, |
| 46 | sequenceBatchSize: idleBatchSize, |
| 47 | reserveNotify: make(chan struct{}, 50), // Buffered to allow multiple allocations without releaseSequenceMonitor |
| 48 | metaKeys: base.DefaultMetadataKeys, |
| 49 | } |
| 50 | |
| 51 | // Set high incr frequency to force batch size increase |
| 52 | oldFrequency := MaxSequenceIncrFrequency |
| 53 | defer func() { MaxSequenceIncrFrequency = oldFrequency }() |
| 54 | MaxSequenceIncrFrequency = 60 * time.Second |
| 55 | |
| 56 | initSequence, err := a.lastSequence(ctx) |
| 57 | assert.Equal(t, uint64(0), initSequence) |
| 58 | assert.NoError(t, err, "error retrieving last sequence") |
| 59 | |
| 60 | // Initial allocation should use batch size of 1 |
| 61 | nextSequence, err := a.nextSequence(ctx) |
| 62 | assert.NoError(t, err) |
| 63 | assert.Equal(t, uint64(1), nextSequence) |
| 64 | assertNewAllocatorStats(t, testStats, 1, 1, 1, 0, nextSequence, 1) |
| 65 | |
| 66 | // Subsequent allocation should increase batch size to 2, allocate 1 |
| 67 | nextSequence, err = a.nextSequence(ctx) |
| 68 | assert.NoError(t, err) |
| 69 | assert.Equal(t, uint64(2), nextSequence) |
| 70 | assertNewAllocatorStats(t, testStats, 2, 3, 2, 0, nextSequence, 3) |
| 71 | |
| 72 | // Subsequent allocation shouldn't trigger allocation |
| 73 | nextSequence, err = a.nextSequence(ctx) |
| 74 | assert.NoError(t, err) |
| 75 | assert.Equal(t, uint64(3), nextSequence) |
| 76 | assertNewAllocatorStats(t, testStats, 2, 3, 3, 0, nextSequence, 3) |
| 77 | |
| 78 | // Subsequent allocation should increase batch to 4, allocate 1 |
| 79 | nextSequence, err = a.nextSequence(ctx) |
| 80 | assert.NoError(t, err) |
| 81 | assert.Equal(t, uint64(4), nextSequence) |
| 82 | assert.Equal(t, 4, int(a.sequenceBatchSize)) |
| 83 | assertNewAllocatorStats(t, testStats, 3, 7, 4, 0, nextSequence, 7) |
| 84 | |
| 85 | // Release unused sequences. Should reduce batch size to 1 (based on 3 unused) |
| 86 | a.releaseUnusedSequences(ctx) |
nothing calls this directly
no test coverage detected