github.com/songzhibin97/gkit@v1.2.13/distributed/backend/backend_redis/redis_test.go (about) 1 package backend_redis 2 3 import ( 4 "strconv" 5 "testing" 6 "time" 7 8 "github.com/songzhibin97/gkit/generator" 9 10 "github.com/songzhibin97/gkit/distributed/task" 11 "github.com/stretchr/testify/assert" 12 13 "github.com/go-redis/redis/v8" 14 "github.com/songzhibin97/gkit/distributed/backend" 15 ) 16 17 func InitBackend() backend.Backend { 18 opt := redis.UniversalOptions{ 19 Addrs: []string{"127.0.0.1:6379"}, 20 } 21 client := redis.NewUniversalClient(&opt) 22 if client == nil { 23 return nil 24 } 25 return NewBackendRedis(client, -1) 26 } 27 28 func TestGroupTakeOver(t *testing.T) { 29 _backend := InitBackend() 30 if _backend == nil { 31 t.Skip() 32 } 33 g := generator.NewSnowflake(time.Now().Local(), 1) 34 ids := make([]string, 0, 3) 35 for i := 0; i < 3; i++ { 36 id, _ := g.NextID() 37 if i == 0 { 38 ids = append(ids, "group:"+strconv.FormatUint(id, 10)) 39 } else { 40 ids = append(ids, "task:"+strconv.FormatUint(id, 10)) 41 } 42 43 } 44 var ( 45 group = task.GroupMeta{ 46 GroupID: ids[0], 47 Name: "group", 48 } 49 task1 = task.Signature{ 50 ID: ids[1], 51 GroupID: group.GroupID, 52 Name: "task1", 53 } 54 task2 = task.Signature{ 55 ID: ids[2], 56 GroupID: group.GroupID, 57 Name: "task2", 58 } 59 ) 60 _ = _backend.ResetGroup(group.GroupID) 61 _ = _backend.ResetTask(task1.ID, task2.ID) 62 isCompleted, err := _backend.GroupCompleted(group.GroupID) 63 if assert.Error(t, err) { 64 assert.False(t, isCompleted) 65 assert.Equal(t, err, redis.Nil) 66 } 67 68 _ = _backend.GroupTakeOver(group.GroupID, group.Name, task1.ID, task2.ID) 69 isCompleted, err = _backend.GroupCompleted(group.GroupID) 70 if assert.Error(t, err) { 71 assert.False(t, isCompleted) 72 assert.Equal(t, err, redis.Nil) 73 } 74 75 _ = _backend.SetStatePending(&task1) 76 _ = _backend.SetStateStarted(&task2) 77 78 isCompleted, err = _backend.GroupCompleted(group.GroupID) 79 if assert.NoError(t, err) { 80 assert.False(t, isCompleted) 81 } 82 result := []*task.Result{{ 83 Type: "int", 84 Value: 1, 85 }} 86 _ = _backend.SetStateStarted(&task1) 87 _ = _backend.SetStateSuccess(&task2, result) 88 isCompleted, err = _backend.GroupCompleted(group.GroupID) 89 if assert.NoError(t, err) { 90 assert.False(t, isCompleted) 91 } 92 _ = _backend.SetStateFailure(&task1, "failure") 93 isCompleted, err = _backend.GroupCompleted(group.GroupID) 94 if assert.NoError(t, err) { 95 assert.True(t, isCompleted) 96 } 97 } 98 99 func TestGetStatus(t *testing.T) { 100 _backend := InitBackend() 101 if _backend == nil { 102 t.Skip() 103 } 104 task1 := task.Signature{ 105 ID: "task1", 106 GroupID: "group", 107 Name: "task1", 108 } 109 _ = _backend.ResetTask(task1.ID) 110 111 status, err := _backend.GetStatus(task1.ID) 112 assert.Equal(t, err, redis.Nil) 113 assert.Nil(t, status) 114 115 _ = _backend.SetStatePending(&task1) 116 status, err = _backend.GetStatus(task1.ID) 117 assert.NoError(t, err) 118 assert.Equal(t, status.Status, task.StatePending) 119 120 _ = _backend.SetStateReceived(&task1) 121 status, err = _backend.GetStatus(task1.ID) 122 assert.NoError(t, err) 123 assert.Equal(t, status.Status, task.StateReceived) 124 125 _ = _backend.SetStateStarted(&task1) 126 status, err = _backend.GetStatus(task1.ID) 127 assert.NoError(t, err) 128 assert.Equal(t, status.Status, task.StateStarted) 129 130 result := &task.Result{ 131 Type: "int", 132 Value: 1, 133 } 134 _ = _backend.SetStateSuccess(&task1, []*task.Result{result}) 135 status, err = _backend.GetStatus(task1.ID) 136 assert.NoError(t, err) 137 assert.Equal(t, status.Status, task.StateSuccess) 138 } 139 140 func TestResult(t *testing.T) { 141 _backend := InitBackend() 142 if _backend == nil { 143 t.Skip() 144 } 145 task1 := task.Signature{ 146 ID: "task1", 147 GroupID: "group", 148 Name: "task1", 149 } 150 _ = _backend.SetStatePending(&task1) 151 status, err := _backend.GetStatus(task1.ID) 152 assert.NoError(t, err) 153 assert.Equal(t, status.Status, task.StatePending) 154 155 _ = _backend.ResetTask(task1.ID) 156 157 status, err = _backend.GetStatus(task1.ID) 158 assert.Equal(t, err, redis.Nil) 159 assert.Nil(t, status) 160 }