@@ -3,115 +3,108 @@ package spanner
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "os"
7
6
"testing"
7
+ "time"
8
8
9
9
"github.com/bitleak/lmstfy/config"
10
+ "github.com/bitleak/lmstfy/engine"
11
+ "github.com/bitleak/lmstfy/storage/persistence/model"
12
+
10
13
"github.com/stretchr/testify/assert"
14
+ "github.com/stretchr/testify/require"
11
15
)
12
16
13
- var (
14
- dummyCtx = context .TODO ()
17
+ const (
18
+ poolName = "test_pool"
19
+ namespace = "test_ns"
15
20
)
16
21
17
- func init () {
18
- if os .Getenv ("SPANNER_EMULATOR_HOST" ) == "" {
19
- panic (fmt .Sprintf ("failed to find $SPANNER_EMULATOR_HOST value" ))
20
- }
21
- err := CreateInstance (dummyCtx , config .SpannerEmulator )
22
- if err != nil {
23
- panic (fmt .Sprintf ("create instance error: %v" , err ))
22
+ func TestSpanner_Basic (t * testing.T ) {
23
+ ctx := context .Background ()
24
+ mgr , err := NewSpanner (config .SpannerEmulator )
25
+ require .NoError (t , err )
26
+
27
+ jobCnt := int64 (10 )
28
+ jobIDs := make ([]string , jobCnt )
29
+ createJobs := make ([]engine.Job , jobCnt )
30
+ for i := int64 (0 ); i < jobCnt ; i ++ {
31
+ queue := "q1"
32
+ if i % 2 == 0 {
33
+ queue = "q2"
34
+ }
35
+ createJobs [i ] = engine .NewJob (namespace , queue , []byte ("hello" ), 10 , 4 , 3 , "" )
36
+ jobIDs [i ] = createJobs [i ].ID ()
24
37
}
25
- err = CreateDatabase (dummyCtx , config .SpannerEmulator )
26
- if err != nil {
27
- panic (fmt .Sprintf ("create db error: %v" , err ))
38
+ require .NoError (t , mgr .BatchAddJobs (ctx , poolName , createJobs ))
39
+
40
+ validateJob := func (t * testing.T , job engine.Job ) {
41
+ assert .NotEmpty (t , job .ID ())
42
+ assert .EqualValues (t , job .Namespace (), namespace )
43
+ assert .EqualValues (t , job .Tries (), 3 )
44
+ assert .GreaterOrEqual (t , job .Delay (), uint32 (1 ))
45
+ assert .LessOrEqual (t , job .Delay (), uint32 (4 ))
46
+ assert .GreaterOrEqual (t , job .TTL (), uint32 (1 ))
47
+ assert .LessOrEqual (t , job .TTL (), uint32 (10 ))
28
48
}
29
- }
30
49
31
- func TestCreateSpannerClient (t * testing.T ) {
32
- _ , err := createSpannerClient (config .SpannerEmulator )
33
- assert .Nil (t , err )
34
- }
50
+ t .Run ("Batch Get Jobs By ID" , func (t * testing.T ) {
51
+ jobs , err := mgr .BatchGetJobsByID (ctx , jobIDs )
52
+ assert .Nil (t , err )
53
+ assert .EqualValues (t , len (jobIDs ), len (jobs ))
54
+ for _ , job := range jobs {
55
+ validateJob (t , job )
56
+ }
57
+ })
35
58
36
- func TestSpanner_BatchAddDelJobs (t * testing.T ) {
37
- mgr , err := NewSpanner (config .SpannerEmulator )
38
- if err != nil {
39
- panic (fmt .Sprintf ("Failed to create spanner client with error: %s" , err ))
40
- }
41
- jobs := createTestJobsData ()
42
- err = mgr .BatchAddJobs (ctx , poolName , jobs )
43
- if err != nil {
44
- panic (fmt .Sprintf ("Failed to add jobs with error: %s" , err ))
45
- }
46
- t .Logf ("add jobs success %v rows" , len (jobs ))
59
+ t .Run ("Get Ready Jobs" , func (t * testing.T ) {
60
+ readyJobs , err := mgr .GetReadyJobs (ctx , & model.DBJobReq {
61
+ PoolName : poolName ,
62
+ ReadyTime : time .Now ().Unix () + 10 ,
63
+ Count : jobCnt ,
64
+ })
65
+ require .NoError (t , err )
66
+ require .EqualValues (t , jobCnt , len (readyJobs ))
67
+ for _ , job := range readyJobs {
68
+ validateJob (t , job )
69
+ }
70
+ })
47
71
48
- count , err := mgr .DelJobs (ctx , jobIDs )
49
- if err != nil {
50
- panic (fmt .Sprintf ("failed to delete job: %v" , err ))
51
- }
52
- t .Logf ("del jobs success %v rows" , count )
53
- }
72
+ t .Run ("Get Queue Size" , func (t * testing.T ) {
73
+ queueSizes , err := mgr .GetQueueSize (ctx , []* model.DBJobReq {
74
+ {PoolName : poolName , Namespace : namespace , Queue : "q1" , ReadyTime : time .Now ().Unix () - 10 , Count : jobCnt },
75
+ {PoolName : poolName , Namespace : namespace , Queue : "q2" , ReadyTime : time .Now ().Unix () - 10 , Count : jobCnt },
76
+ })
77
+ require .NoError (t , err )
78
+ assert .EqualValues (t , jobCnt / 2 , queueSizes [fmt .Sprintf ("%s/%s" , namespace , "q1" )])
79
+ assert .EqualValues (t , jobCnt / 2 , queueSizes [fmt .Sprintf ("%s/%s" , namespace , "q2" )])
80
+ })
54
81
55
- func TestSpanner_BatchGetJobs (t * testing.T ) {
56
- mgr , err := NewSpanner (config .SpannerEmulator )
57
- if err != nil {
58
- panic (fmt .Sprintf ("Failed to create spanner client with error: %s" , err ))
59
- }
60
- jobs := createTestJobsData ()
61
- mgr .BatchAddJobs (ctx , poolName , jobs )
62
- req := createTestReqData ()
63
- jobs , err = mgr .BatchGetJobs (ctx , req )
64
- if err != nil {
65
- panic (fmt .Sprintf ("BatchGetJobs failed with error: %s" , err ))
66
- }
67
- assert .EqualValues (t , 3 , len (jobs ))
68
- mgr .DelJobs (ctx , jobIDs )
82
+ t .Run ("Del Jobs" , func (t * testing.T ) {
83
+ count , err := mgr .DelJobs (context .Background (), jobIDs )
84
+ require .NoError (t , err )
85
+ require .EqualValues (t , jobCnt , count )
86
+ })
69
87
}
70
88
71
- func TestSpanner_GetQueueSize (t * testing.T ) {
89
+ func TestSpanner_NoExpiredJob (t * testing.T ) {
90
+ ctx := context .Background ()
72
91
mgr , err := NewSpanner (config .SpannerEmulator )
73
- if err != nil {
74
- panic (fmt .Sprintf ("Failed to create spanner client with error: %s" , err ))
75
- }
76
- jobs := createTestJobsData ()
77
- mgr .BatchAddJobs (ctx , poolName , jobs )
78
- req := createTestReqData ()
79
- count , err := mgr .GetQueueSize (ctx , req )
80
- if err != nil || len (count ) == 0 {
81
- panic (fmt .Sprintf ("BatchGetJobs failed with error: %s" , err ))
82
- }
83
- key1 , key2 := fmt .Sprintf ("%s/%s" , "n1" , "q1" ), fmt .Sprintf ("%s/%s" , "n1" , "q2" )
84
- assert .EqualValues (t , 2 , count [key1 ])
85
- assert .EqualValues (t , 1 , count [key2 ])
86
- mgr .DelJobs (ctx , jobIDs )
87
- }
92
+ require .NoError (t , err )
88
93
89
- func TestSpanner_GetReadyJobs (t * testing.T ) {
90
- mgr , err := NewSpanner (config .SpannerEmulator )
91
- if err != nil {
92
- panic (fmt .Sprintf ("Failed to create spanner client with error: %s" , err ))
93
- }
94
- jobs := createTestJobsData ()
95
- mgr .BatchAddJobs (ctx , poolName , jobs )
96
- req := createTestReqData2 ()
97
- jobs , err = mgr .GetReadyJobs (ctx , req )
98
- if err != nil {
99
- panic (fmt .Sprintf ("GetReadyJobs failed with error: %s" , err ))
94
+ jobCnt := int64 (10 )
95
+ jobIDs := make ([]string , jobCnt )
96
+ createJobs := make ([]engine.Job , jobCnt )
97
+ for i := int64 (0 ); i < jobCnt ; i ++ {
98
+ queue := "q3"
99
+ createJobs [i ] = engine .NewJob (namespace , queue , []byte ("hello" ), 0 , 4 , 3 , "" )
100
+ jobIDs [i ] = createJobs [i ].ID ()
100
101
}
101
- assert .EqualValues (t , 2 , len (jobs ))
102
- mgr .DelJobs (ctx , jobIDs )
103
- }
102
+ require .NoError (t , mgr .BatchAddJobs (ctx , poolName , createJobs ))
104
103
105
- func TestSpanner_BatchGetJobsByID (t * testing.T ) {
106
- mgr , err := NewSpanner (config .SpannerEmulator )
107
- if err != nil {
108
- panic (fmt .Sprintf ("Failed to create spanner client with error: %s" , err ))
109
- }
110
- jobs := createTestJobsData ()
111
- mgr .BatchAddJobs (ctx , poolName , jobs )
112
- IDs := []string {"1" , "2" , "3" }
113
- jobs , err = mgr .BatchGetJobsByID (ctx , IDs )
104
+ jobs , err := mgr .BatchGetJobsByID (ctx , jobIDs )
114
105
assert .Nil (t , err )
115
- assert .EqualValues (t , 3 , len (jobs ))
116
- mgr .DelJobs (ctx , jobIDs )
106
+ assert .EqualValues (t , len (jobIDs ), len (jobs ))
107
+ for _ , job := range jobs {
108
+ assert .EqualValues (t , job .TTL (), 0 )
109
+ }
117
110
}
0 commit comments