-
Notifications
You must be signed in to change notification settings - Fork 7
/
utilities_test.go
133 lines (115 loc) · 3.29 KB
/
utilities_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
package main
import (
"fmt"
"strings"
"testing"
"time"
"github.com/alexjlockwood/gcm"
)
func TestAppendAttempts(t *testing.T) {
a := runReport.Attempts
appendAttempts(2)
if runReport.Attempts != a+2 {
t.Fatalf("Append attempts should be %d, is %d", a+2, runReport.Attempts)
}
}
func TestAppendFailures(t *testing.T) {
a := runReport.Failures
appendFailures(3)
if runReport.Failures != a+3 {
t.Fatalf("Append failures should be %d, is %d", a+3, runReport.Failures)
}
}
func TestAppendCanonicals(t *testing.T) {
a := runReport.Canonicals
appendCanonicals(1)
if runReport.Canonicals != a+1 {
t.Fatalf("Append canonicals should be %d, is %d", a+1, runReport.Canonicals)
}
}
func TestAppendNotRegistered(t *testing.T) {
a := runReport.NotRegistered
appendNotRegistered(1)
if runReport.NotRegistered != a+1 {
t.Fatalf("Append notregistered should be %d, is %d", a+1, runReport.NotRegistered)
}
}
func TestIncrementPending(t *testing.T) {
a := runReport.Pending
incrementPending()
if runReport.Pending != a+1 {
t.Fatalf("Increment pending should be %d, is %d", a+1, runReport.Pending)
}
}
func TestDecrementPending(t *testing.T) {
a := runReport.Pending
decrementPending()
if runReport.Pending != a-1 {
t.Fatalf("Decrement pending should be %d, is %d", a-1, runReport.Pending)
}
}
func TestHandleCanonicalsInResult(t *testing.T) {
canonicalReplacements = nil
var results []gcm.Result
for i := 0; i < 4; i++ {
g := gcm.Result{"asdf", fmt.Sprintf("%d-%d-%d-%d", i, i, i, i), ""}
results = append(results, g)
//gcm.Response{1, 1, 0, 1, gs}
}
// "Handle" the results
handleCanonicalsInResult("asdf", results)
// Now parse through them
for i, r := range canonicalReplacements {
if r.Original != "asdf" {
t.Fatal("Original is not \"asdf\" as it should be")
replacement := fmt.Sprintf("%d-%d-%d-%d", i, i, i, i)
if r.Canonical != replacement {
t.Fatalf("Canonical is wrong. Expecting: %s, got: %s", replacement, r.Canonical)
}
}
}
}
func TestSendMessageToGCM(t *testing.T) {
// Test empty token
ok, err := sendMessageToGCM([]string{}, "")
if ok {
t.Fatal("ok should be false")
}
if err.Error() != "No tokens were supplied, exiting" {
t.Fatalf("Expecting 'No tokens were supplied, exiting', got: %s", err.Error())
}
// Test empty payload
ok, err = sendMessageToGCM([]string{"asdf"}, "")
if ok {
t.Fatal("ok should be false")
}
if err.Error() != "Payload was empty, exiting" {
t.Fatalf("Expecting 'Payload was empty, exiting', got: %s", err.Error())
}
// Test bad json
ok, err = sendMessageToGCM([]string{"asdf"}, "asdf")
if ok {
t.Fatal("ok should be false")
}
if !strings.HasPrefix(err.Error(), "invalid character") {
t.Fatalf("Unexpected error string: %s", err.Error())
}
// Test bad send
aOrig := runReport.Attempts
fOrig := runReport.Failures
ok, err = sendMessageToGCM([]string{"asdf"}, "{\"key\": \"value\"}")
time.Sleep(1 * time.Second)
if ok {
t.Fatal("ok should be false")
}
if runReport.Attempts != aOrig+1 {
t.Fatal("Attempts not incremented by 1")
}
if runReport.Failures != fOrig+2 {
// Plus 2 because of the default retry
t.Fatalf("Failures not incremented by 2 (orig: %d, new: %d)", fOrig, runReport.Failures)
}
if !strings.HasPrefix(err.Error(), "401 error") {
t.Fatalf("Unexpected error string: %s", err.Error())
}
}