mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-27 00:02:19 +08:00
Refactor (*rdb).moveAll test
This commit is contained in:
parent
d2a6cc127d
commit
4fd82c5975
89
rdb_test.go
89
rdb_test.go
@ -18,6 +18,12 @@ func init() {
|
|||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sortStrOpt = cmp.Transformer("SortStr", func(in []string) []string {
|
||||||
|
out := append([]string(nil), in...) // Copy input to avoid mutating it
|
||||||
|
sort.Strings(out)
|
||||||
|
return out
|
||||||
|
})
|
||||||
|
|
||||||
// setup connects to a redis database and flush all keys
|
// setup connects to a redis database and flush all keys
|
||||||
// before returning an instance of rdb.
|
// before returning an instance of rdb.
|
||||||
func setup(t *testing.T) *rdb {
|
func setup(t *testing.T) *rdb {
|
||||||
@ -118,31 +124,76 @@ func TestDequeue(t *testing.T) {
|
|||||||
|
|
||||||
func TestMoveAll(t *testing.T) {
|
func TestMoveAll(t *testing.T) {
|
||||||
r := setup(t)
|
r := setup(t)
|
||||||
seed := []*taskMessage{
|
t1 := randomTask("send_email", "default", nil)
|
||||||
randomTask("send_email", "default", nil),
|
t2 := randomTask("export_csv", "csv", nil)
|
||||||
randomTask("export_csv", "csv", nil),
|
t3 := randomTask("sync_stuff", "sync", nil)
|
||||||
randomTask("sync_stuff", "sync", nil),
|
json1, err := json.Marshal(t1)
|
||||||
}
|
|
||||||
for _, task := range seed {
|
|
||||||
bytes, err := json.Marshal(task)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := client.LPush(inProgress, string(bytes)).Err(); err != nil {
|
json2, err := json.Marshal(t2)
|
||||||
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
json3, err := json.Marshal(t3)
|
||||||
|
|
||||||
err := r.moveAll(inProgress, defaultQueue)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("moveAll(%q, %q) = %v, want nil", inProgress, defaultQueue, err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if l := client.LLen(inProgress).Val(); l != 0 {
|
tests := []struct {
|
||||||
t.Errorf("LLEN %q = %d, want 0", inProgress, l)
|
beforeSrc []string
|
||||||
|
beforeDst []string
|
||||||
|
afterSrc []string
|
||||||
|
afterDst []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
beforeSrc: []string{string(json1), string(json2), string(json3)},
|
||||||
|
beforeDst: []string{},
|
||||||
|
afterSrc: []string{},
|
||||||
|
afterDst: []string{string(json1), string(json2), string(json3)},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
beforeSrc: []string{},
|
||||||
|
beforeDst: []string{string(json1), string(json2), string(json3)},
|
||||||
|
afterSrc: []string{},
|
||||||
|
afterDst: []string{string(json1), string(json2), string(json3)},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
beforeSrc: []string{string(json2), string(json3)},
|
||||||
|
beforeDst: []string{string(json1)},
|
||||||
|
afterSrc: []string{},
|
||||||
|
afterDst: []string{string(json1), string(json2), string(json3)},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
// clean up db before each test case.
|
||||||
|
if err := client.FlushDB().Err(); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// seed src list.
|
||||||
|
for _, msg := range tc.beforeSrc {
|
||||||
|
client.LPush(inProgress, msg)
|
||||||
|
}
|
||||||
|
// seed dst list.
|
||||||
|
for _, msg := range tc.beforeDst {
|
||||||
|
client.LPush(defaultQueue, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.moveAll(inProgress, defaultQueue); err != nil {
|
||||||
|
t.Errorf("(*rdb).moveAll(%q, %q) = %v, want nil", inProgress, defaultQueue, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
gotSrc := client.LRange(inProgress, 0, -1).Val()
|
||||||
|
if diff := cmp.Diff(tc.afterSrc, gotSrc, sortStrOpt); diff != "" {
|
||||||
|
t.Errorf("mismatch found in %q (-want, +got)\n%s", inProgress, diff)
|
||||||
|
}
|
||||||
|
gotDst := client.LRange(defaultQueue, 0, -1).Val()
|
||||||
|
if diff := cmp.Diff(tc.afterDst, gotDst, sortStrOpt); diff != "" {
|
||||||
|
t.Errorf("mismatch found in %q (-want, +got)\n%s", defaultQueue, diff)
|
||||||
}
|
}
|
||||||
if l := client.LLen(defaultQueue).Val(); int(l) != len(seed) {
|
|
||||||
t.Errorf("LLEN %q = %d, want %d", defaultQueue, l, len(seed))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,12 +240,6 @@ func TestForward(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
sortStrOpt := cmp.Transformer("SortStr", func(in []string) []string {
|
|
||||||
out := append([]string(nil), in...) // Copy input to avoid mutating it
|
|
||||||
sort.Strings(out)
|
|
||||||
return out
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
// clean up db before each test case.
|
// clean up db before each test case.
|
||||||
if err := client.FlushDB().Err(); err != nil {
|
if err := client.FlushDB().Err(); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user