github.com/kaydxh/golang@v0.0.131/pkg/pool/taskqueue/pool_test.go (about) 1 package taskqueue_test 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "sync" 7 "testing" 8 "time" 9 10 "github.com/go-redis/redis/v8" 11 redis_ "github.com/kaydxh/golang/pkg/database/redis" 12 taskq_ "github.com/kaydxh/golang/pkg/pool/taskqueue" 13 queue_ "github.com/kaydxh/golang/pkg/pool/taskqueue/queue" 14 redisq_ "github.com/kaydxh/golang/pkg/pool/taskqueue/queue/redis" 15 viper_ "github.com/kaydxh/golang/pkg/viper" 16 "golang.org/x/net/context" 17 ) 18 19 type TaskA struct { 20 } 21 22 type TaskAArgs struct { 23 Param1 string 24 Param2 int 25 } 26 27 type TaskAResult struct { 28 Result1 string 29 Result2 int 30 } 31 32 func (t TaskA) Scheme() string { 33 return "taskA" 34 } 35 36 func (t TaskA) TaskHandler(ctx context.Context, msg *queue_.Message) (*queue_.MessageResult, error) { 37 38 time.Sleep(30 * time.Second) 39 var args TaskAArgs 40 err := json.Unmarshal([]byte(msg.Args), &args) 41 if err != nil { 42 return nil, err 43 } 44 45 //fmt.Printf("get args: %v\n", args) 46 fmt.Printf("process task id: %v, name: %v\n", msg.Id, msg.Name) 47 48 result := &queue_.MessageResult{ 49 Id: msg.Id, 50 InnerId: msg.InnerId, 51 Name: msg.Name, 52 Scheme: msg.Scheme, 53 } 54 55 taskResult := &TaskAResult{ 56 Result1: "Result1", 57 Result2: 2, 58 } 59 60 data, err := json.Marshal(taskResult) 61 if err != nil { 62 return nil, err 63 } 64 result.Result = string(data) 65 return result, nil 66 } 67 68 var ( 69 once sync.Once 70 db *redis.Client 71 err error 72 ) 73 74 func GetDBOrDie() *redis.Client { 75 once.Do(func() { 76 cfgFile := "./taskq.yaml" 77 config := redis_.NewConfig(redis_.WithViper(viper_.GetViper(cfgFile, "database.redis"))) 78 79 db, err = config.Complete().New(context.Background()) 80 if err != nil { 81 panic(err) 82 } 83 if db == nil { 84 panic("db is not enable") 85 } 86 }) 87 88 return db 89 } 90 91 func TestTaskQueue(t *testing.T) { 92 93 taskq_.Register(TaskA{}) 94 95 db := GetDBOrDie() 96 97 redisq := redisq_.NewQueue(db, queue_.QueueOptions{ 98 Name: "redis", 99 }) 100 101 pool := taskq_.NewPool(redisq, taskq_.WithFetcherBurst(1), taskq_.WithWorkTimeout(-1), taskq_.WithResultCallbackFunc(func(ctx context.Context, result *queue_.MessageResult) { 102 t.Logf("--> callback fetch result %v of msg", result) 103 })) 104 ctx := context.Background() 105 err = pool.Consume(ctx) 106 if err != nil { 107 t.Errorf("failed to consume task, err: %v", err) 108 return 109 } 110 111 args := TaskAArgs{ 112 Param1: "param1", 113 Param2: 10, 114 } 115 116 data, err := json.Marshal(args) 117 if err != nil { 118 t.Errorf("failed to marshal args, err: %v", err) 119 return 120 } 121 122 var ( 123 ids []string 124 wg sync.WaitGroup 125 ) 126 for i := 0; i < 10; i++ { 127 msg := &queue_.Message{ 128 Scheme: "taskA", 129 Args: string(data), 130 } 131 wg.Add(1) 132 go func(i int, m *queue_.Message) { 133 defer wg.Done() 134 m.Name = fmt.Sprintf("taskA-%v", i) 135 id, err := pool.Publish(ctx, m) 136 if err != nil { 137 t.Errorf("failed to pulibsh task, err: %v", err) 138 return 139 } 140 t.Logf("pulibsh task, innerId: %v, msg: %v", id, m) 141 ids = append(ids, id) 142 }(i, msg) 143 } 144 145 wg.Wait() 146 147 // time.Sleep(5 * time.Second) 148 149 /* 150 for _, id := range ids { 151 result, err := pool.FetchResult(ctx, id) 152 if err != nil { 153 t.Errorf("%v", err) 154 continue 155 } 156 t.Logf("fetch result %v of msg %v", result, id) 157 } 158 */ 159 select {} 160 161 } 162 163 func TestTaskQueueServer(t *testing.T) { 164 165 GetDBOrDie() 166 taskq_.Register(TaskA{}) 167 cfgFile := "./taskq.yaml" 168 config := taskq_.NewConfig(taskq_.WithViper(viper_.GetViper(cfgFile, "taskqueue"))) 169 170 ctx := context.Background() 171 pool, err := config.Complete().New(ctx, 172 taskq_.WithResultCallbackFunc(func(ctx context.Context, result *queue_.MessageResult) { 173 t.Logf("--> callback fetch result %v of msg", result) 174 })) 175 if err != nil { 176 t.Errorf("failed to install taskqueue, err: %v", err) 177 return 178 } 179 if pool == nil { 180 return 181 } 182 183 args := TaskAArgs{ 184 Param1: "param1", 185 Param2: 10, 186 } 187 188 data, err := json.Marshal(args) 189 if err != nil { 190 t.Errorf("failed to marshal args, err: %v", err) 191 return 192 } 193 194 var ( 195 ids []string 196 wg sync.WaitGroup 197 ) 198 for i := 0; i < 10; i++ { 199 msg := &queue_.Message{ 200 Scheme: "taskA", 201 Args: string(data), 202 } 203 wg.Add(1) 204 go func(i int, m *queue_.Message) { 205 defer wg.Done() 206 m.Name = fmt.Sprintf("taskA-%v", i) 207 id, err := pool.Publish(ctx, m) 208 if err != nil { 209 t.Errorf("failed to pulibsh task, err: %v", err) 210 return 211 } 212 t.Logf("pulibsh task, innerId: %v, msg: %v", id, m) 213 ids = append(ids, id) 214 }(i, msg) 215 } 216 217 wg.Wait() 218 219 select {} 220 }