Skip to content

Commit

Permalink
fix: add coverage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WqyJh committed Jan 26, 2024
1 parent 8cafd1d commit e71837c
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func (c *Client) strongFetchBatch(ctx context.Context, keys []string, expire tim
select {
case <-ctx.Done():
ch <- pair{idx: i, err: ctx.Err()}
return
case <-ticker.C:
// equal to time.Sleep(c.Options.LockSleep) but can be canceled
}
Expand Down
67 changes: 67 additions & 0 deletions batch_cover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,70 @@ func TestTagAsDeletedBatchWait(t *testing.T) {
assert.Error(t, err, fmt.Errorf("wait replicas 1 failed. result replicas: 0"))
}
}

func TestWeakFetchBatchCanceled(t *testing.T) {
clearCache()
rc := NewClient(rdb, NewDefaultOptions())
n := int(rand.Int31n(20) + 10)
idxs := genIdxs(n)
keys, values1, values2 := genKeys(idxs), genValues(n, "value_"), genValues(n, "eulav_")
values3 := genValues(n, "vvvv_")
go func() {
dc2 := NewClient(rdb, NewDefaultOptions())
v, err := dc2.FetchBatch(keys, 60*time.Second, genBatchDataFunc(values1, 400))
assert.Nil(t, err)
assert.Equal(t, values1, v)
}()
time.Sleep(20 * time.Millisecond)

began := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
_, err := rc.FetchBatch2(ctx, keys, 60*time.Second, genBatchDataFunc(values2, 200))
assert.ErrorIs(t, err, context.DeadlineExceeded)
assert.True(t, time.Since(began) < time.Duration(110)*time.Millisecond)

ctx, cancel = context.WithCancel(context.Background())
go func() {
time.Sleep(100 * time.Millisecond)
cancel()
}()
began = time.Now()
_, err = rc.FetchBatch2(ctx, keys, 60*time.Second, genBatchDataFunc(values3, 200))
assert.ErrorIs(t, err, context.Canceled)
assert.True(t, time.Since(began) < time.Duration(110)*time.Millisecond)
}

func TestStrongFetchBatchCanceled(t *testing.T) {
clearCache()
rc := NewClient(rdb, NewDefaultOptions())
rc.Options.StrongConsistency = true
n := int(rand.Int31n(20) + 10)
idxs := genIdxs(n)
keys, values1, values2 := genKeys(idxs), genValues(n, "value_"), genValues(n, "eulav_")
values3 := genValues(n, "vvvv_")
go func() {
dc2 := NewClient(rdb, NewDefaultOptions())
v, err := dc2.FetchBatch(keys, 60*time.Second, genBatchDataFunc(values1, 400))
assert.Nil(t, err)
assert.Equal(t, values1, v)
}()
time.Sleep(20 * time.Millisecond)

began := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
_, err := rc.FetchBatch2(ctx, keys, 60*time.Second, genBatchDataFunc(values2, 200))
assert.ErrorIs(t, err, context.DeadlineExceeded)
assert.True(t, time.Since(began) < time.Duration(110)*time.Millisecond)

ctx, cancel = context.WithCancel(context.Background())
go func() {
time.Sleep(100 * time.Millisecond)
cancel()
}()
began = time.Now()
_, err = rc.FetchBatch2(ctx, keys, 60*time.Second, genBatchDataFunc(values3, 200))
assert.ErrorIs(t, err, context.Canceled)
assert.True(t, time.Since(began) < time.Duration(110)*time.Millisecond)
}
62 changes: 62 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,37 @@ func TestStrongErrorFetch(t *testing.T) {
assert.True(t, time.Since(began) < time.Duration(150)*time.Millisecond)
}

func TestStrongFetchCanceled(t *testing.T) {
clearCache()
rc := NewClient(rdb, NewDefaultOptions())
rc.Options.StrongConsistency = true
expected := "value1"
go func() {
dc2 := NewClient(rdb, NewDefaultOptions())
v, err := dc2.Fetch(rdbKey, 60*time.Second, genDataFunc(expected, 400))
assert.Nil(t, err)
assert.Equal(t, expected, v)
}()
time.Sleep(20 * time.Millisecond)

began := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
_, err := rc.Fetch2(ctx, rdbKey, 60*time.Second, genDataFunc(expected, 200))
assert.ErrorIs(t, err, context.DeadlineExceeded)
assert.True(t, time.Since(began) < time.Duration(110)*time.Millisecond)

ctx, cancel = context.WithCancel(context.Background())
go func() {
time.Sleep(100 * time.Millisecond)
cancel()
}()
began = time.Now()
_, err = rc.Fetch2(ctx, rdbKey, 60*time.Second, genDataFunc(expected, 200))
assert.ErrorIs(t, err, context.Canceled)
assert.True(t, time.Since(began) < time.Duration(110)*time.Millisecond)
}

func TestWeakErrorFetch(t *testing.T) {
rc := NewClient(rdb, NewDefaultOptions())

Expand All @@ -165,6 +196,37 @@ func TestWeakErrorFetch(t *testing.T) {
assert.True(t, time.Since(began) < time.Duration(150)*time.Millisecond)
}

func TestWeakFetchCanceled(t *testing.T) {
rc := NewClient(rdb, NewDefaultOptions())

clearCache()
expected := "value1"
go func() {
dc2 := NewClient(rdb, NewDefaultOptions())
v, err := dc2.Fetch(rdbKey, 60*time.Second, genDataFunc(expected, 400))
assert.Nil(t, err)
assert.Equal(t, expected, v)
}()
time.Sleep(20 * time.Millisecond)

began := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
_, err := rc.Fetch2(ctx, rdbKey, 60*time.Second, genDataFunc(expected, 200))
assert.ErrorIs(t, err, context.DeadlineExceeded)
assert.True(t, time.Since(began) < time.Duration(110)*time.Millisecond)

ctx, cancel = context.WithCancel(context.Background())
go func() {
time.Sleep(100 * time.Millisecond)
cancel()
}()
began = time.Now()
_, err = rc.Fetch2(ctx, rdbKey, 60*time.Second, genDataFunc(expected, 200))
assert.ErrorIs(t, err, context.Canceled)
assert.True(t, time.Since(began) < time.Duration(110)*time.Millisecond)
}

func TestRawGet(t *testing.T) {
rc := NewClient(rdb, NewDefaultOptions())
_, err := rc.RawGet(ctx, "not-exists")
Expand Down

0 comments on commit e71837c

Please sign in to comment.