-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_syncmap_test.go
190 lines (129 loc) · 3.25 KB
/
database_syncmap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package offline
import (
"context"
"encoding/json"
"sync/atomic"
"testing"
"time"
)
func TestSyncMapDatabase(t *testing.T) {
ctx := context.Background()
db_uri := "syncmap://"
db, err := NewDatabase(ctx, db_uri)
if err != nil {
t.Fatalf("Failed to create new database, %v", err)
}
job_type := "testing"
instructions := map[string]interface{}{
"name": "testing",
"id": 1234,
}
enc_instructions, err := json.Marshal(instructions)
if err != nil {
t.Fatalf("Failed to marshal instructions, %v", err)
}
str_instructions := string(enc_instructions)
job, err := NewJob(ctx, "testing", job_type, str_instructions)
if err != nil {
t.Fatalf("Failed to create new job, %v", err)
}
err = db.AddJob(ctx, job)
if err != nil {
t.Fatalf("Failed to add job, %v", err)
}
job, err = db.GetJob(ctx, job.Id)
if err != nil {
t.Fatalf("Failed to retrieve job, %v", err)
}
if job.Type != job_type {
t.Fatalf("Invalid job type. Expected '%s' but got '%s'", job_type, job.Type)
}
str_instructions = job.Instructions
err = json.Unmarshal([]byte(str_instructions), &instructions)
if err != nil {
t.Fatalf("Failed to unmarshal job instructions, %v", err)
}
v, ok := instructions["id"]
if !ok {
t.Fatalf("Unable to find 'id' key in job.Instructions")
}
switch v.(type) {
case float64:
// pass
default:
t.Fatalf("Unexpected type for id, %T", v)
}
if int(v.(float64)) != 1234 {
t.Fatalf("Unexpected value for id")
}
job.Status = Processing
err = db.UpdateJob(ctx, job)
if err != nil {
t.Fatalf("Failed to update job, %v", err)
}
job, err = db.GetJob(ctx, job.Id)
if err != nil {
t.Fatalf("Failed to retrieve job (again), %v", err)
}
if job.Status != Processing {
t.Fatalf("Expected job to be processed but is: %v", job.Status)
}
err = db.RemoveJob(ctx, job)
if err != nil {
t.Fatalf("Failed to delete job")
}
job, _ = db.GetJob(ctx, job.Id)
if job != nil {
t.Fatalf("Expected to not find job (after deleting) but it's still there")
}
}
func TestPruneAndListJobs(t *testing.T) {
ctx := context.Background()
db_uri := "syncmap://"
db, err := NewDatabase(ctx, db_uri)
if err != nil {
t.Fatalf("Failed to create new database, %v", err)
}
job_type := "testing"
instructions := map[string]interface{}{
"name": "testing",
"id": 1234,
}
for i := 0; i < 5; i++ {
enc_instructions, err := json.Marshal(instructions)
if err != nil {
t.Fatalf("Failed to marshal instructions, %v", err)
}
str_instructions := string(enc_instructions)
job, err := NewJob(ctx, "testing", job_type, str_instructions)
if err != nil {
t.Fatalf("Failed to create new job, %v", err)
}
err = db.AddJob(ctx, job)
if err != nil {
t.Fatalf("Failed to add job, %v", err)
}
}
now := time.Now()
ts := now.Unix()
err = db.PruneJobs(ctx, Pending, ts)
if err != nil {
t.Fatalf("Failed to prune jobs, %v", err)
}
count := int32(0)
list_cb := func(ctx context.Context, job *Job) error {
atomic.AddInt32(&count, 1)
return nil
}
err = db.ListJobs(ctx, list_cb)
if err != nil {
t.Fatalf("Failed to list jobs, %v", err)
}
if count != 0 {
t.Fatalf("Expecte job count to be 0, not %d", count)
}
err = db.Close(ctx)
if err != nil {
t.Fatalf("Failed to close connection, %v", err)
}
}