github.com/songzhibin97/gkit@v1.2.13/distributed/service_test.go (about) 1 package distributed 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/songzhibin97/gkit/log" 8 9 "github.com/stretchr/testify/assert" 10 11 "github.com/songzhibin97/gkit/distributed/backend/backend_redis" 12 13 "github.com/go-redis/redis/v8" 14 "github.com/songzhibin97/gkit/distributed/broker" 15 "github.com/songzhibin97/gkit/distributed/controller/controller_redis" 16 "github.com/songzhibin97/gkit/distributed/locker/lock_ridis" 17 ) 18 19 func initServer() *Server { 20 opt := redis.UniversalOptions{ 21 Addrs: []string{"127.0.0.1:6379"}, 22 } 23 client := redis.NewUniversalClient(&opt) 24 if client == nil { 25 return nil 26 } 27 lock := lock_ridis.NewRedisLock(client) 28 bk := broker.NewBroker(broker.NewRegisteredTask(), context.Background()) 29 c := controller_redis.NewControllerRedis(bk, client, "test_task", "delayed") 30 backend := backend_redis.NewBackendRedis(client, -1) 31 return NewServer(c, backend, lock, log.NewHelper(log.With(log.DefaultLogger)), nil) 32 } 33 34 func TestRegisterTasks(t *testing.T) { 35 t.Parallel() 36 s := initServer() 37 _, ok := s.GetRegisteredTask("test_task") 38 assert.False(t, ok) 39 err := s.RegisteredTasks(map[string]interface{}{ 40 "test_task": func() error { return nil }, 41 }) 42 assert.NoError(t, err) 43 44 _, ok = s.GetRegisteredTask("test_task") 45 assert.True(t, ok) 46 } 47 48 func TestRegisterTask(t *testing.T) { 49 t.Parallel() 50 s := initServer() 51 _, ok := s.GetRegisteredTask("test_task") 52 assert.False(t, ok) 53 54 err := s.RegisteredTask("test_task", func() error { return nil }) 55 assert.NoError(t, err) 56 57 _, ok = s.GetRegisteredTask("test_task") 58 assert.True(t, ok) 59 }