-
Notifications
You must be signed in to change notification settings - Fork 8
/
gowork_test.go
176 lines (136 loc) · 3.52 KB
/
gowork_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
package gowork
import (
"reflect"
"testing"
)
const (
ERROR_MSG string = "AHH ERROR HAPPENED"
EVENT_ID string = "123abc"
SECRET_STR_INVALID string = "GoWork"
SECRET_STR_VALID string = "GoWorkGoWorkGoWorkGoWorkGoWork12"
)
func TestNewEventError(t *testing.T) {
var (
ofType string
)
t.Parallel()
e := NewEventError(ERROR_MSG)
ofType = reflect.TypeOf(e).String()
if ofType != "*gowork.Event" {
t.Fatalf("Expected type of *Event, received %s instead.", ofType)
}
if e.Error != ERROR_MSG {
t.Fatalf("Expected error message %s, received %s instead.", ERROR_MSG, e.Error)
}
}
func TestNewEventWork(t *testing.T) {
var (
ofType string
)
t.Parallel()
w := NewEventWork(&Work{})
ofType = reflect.TypeOf(w).String()
if ofType != "*gowork.Event" {
t.Fatalf("Expected type of *Event, received %s instead.", ofType)
}
}
func TestNewEventWorker(t *testing.T) {
var (
ofType string
)
t.Parallel()
w := NewEventWorker(&Worker{})
ofType = reflect.TypeOf(w).String()
if ofType != "*gowork.Event" {
t.Fatalf("Expected type of *Event, received %s instead.", ofType)
}
}
func TestNewServer(t *testing.T) {
var (
err error
)
t.Parallel()
if _, err = NewServer(SECRET_STR_VALID); err != nil {
t.Fatal(err)
}
}
func TestNewServerInvalidSecretSize(t *testing.T) {
var (
err error
)
t.Parallel()
if _, err = NewServer(SECRET_STR_INVALID); err == nil {
t.Fatalf("Expected NewServer to throw a secret length error. Secret %s was passed in.", SECRET_STR_INVALID)
}
}
func TestMustNewServer(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("TestMustNewServer paniced when it shouldn't have. Recovered %v", r)
}
}()
t.Parallel()
MustNewServer(SECRET_STR_VALID)
}
func TestMustNewServerPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
return
} else {
t.Fatalf("TestMustNewServer did not panic when it should have. Secret %s was passed in.", SECRET_STR_INVALID)
}
}()
t.Parallel()
MustNewServer(SECRET_STR_INVALID)
}
func TestNewHandler(t *testing.T) {
var (
err error
ws *WorkServer = MustNewServer(SECRET_STR_VALID)
)
t.Parallel()
if err = ws.NewHandler(EVENT_ID, func(*Event, map[string]interface{}) {}); err != nil {
t.Fatal(err)
}
if _, ok := ws.Handlers[EVENT_ID]; !ok {
t.Fatalf("Expected event_id %s to be set as a handler, it was not", EVENT_ID)
}
}
func TestNewHandlerAlreadyExists(t *testing.T) {
var (
err error
ws *WorkServer = MustNewServer(SECRET_STR_VALID)
)
t.Parallel()
if err = ws.NewHandler(EVENT_ID, func(*Event, map[string]interface{}) {}); err != nil {
t.Fatal(err)
}
if err = ws.NewHandler(EVENT_ID, func(*Event, map[string]interface{}) {}); err == nil {
t.Fatalf("Expected NewHandler to throw a 'Handler already exists' error. Event ID %s was passed in.", EVENT_ID)
}
}
func TestAddParams(t *testing.T) {
var (
ws *WorkServer = MustNewServer(SECRET_STR_VALID)
params map[string]interface{} = map[string]interface{}{
"key": "value",
"key2": 12,
}
)
t.Parallel()
ws = ws.AddParams(params)
if ws.HandlerParams["key"].(string) != "value" {
t.Fatal("AddParams failed to set HandlerParams properly.")
}
if ws.HandlerParams["key2"].(int) != 12 {
t.Fatal("AddParams failed to set HandlerParams properly.")
}
}
func TestQueueSize(t *testing.T) {
var (
ws *WorkServer = MustNewServer(SECRET_STR_VALID)
)
if ws.QueueSize() != ws.Queue.Size() {
t.Fatalf("Expected QueueSize to return %d, it returned %d instead.", ws.Queue.Size(), ws.QueueSize())
}
}