github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/custom_event_test.go (about) 1 package internal 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "testing" 7 "time" 8 ) 9 10 var ( 11 now = time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC) 12 strLen512 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 13 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 14 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 15 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 16 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 17 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 18 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 19 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20 strLen255 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 21 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 22 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + 23 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 24 ) 25 26 // Tests use a single key-value pair in params to ensure deterministic JSON 27 // ordering. 28 29 func TestCreateCustomEventSuccess(t *testing.T) { 30 event, err := CreateCustomEvent("myEvent", map[string]interface{}{"alpha": 1}, now) 31 if nil != err { 32 t.Fatal(err) 33 } 34 js, err := json.Marshal(event) 35 if nil != err { 36 t.Fatal(err) 37 } 38 if string(js) != `[{"type":"myEvent","timestamp":1.41713646e+09},{"alpha":1},{}]` { 39 t.Fatal(string(js)) 40 } 41 } 42 43 func TestInvalidEventTypeCharacter(t *testing.T) { 44 event, err := CreateCustomEvent("myEvent!", map[string]interface{}{"alpha": 1}, now) 45 if err != ErrEventTypeRegex { 46 t.Fatal(err) 47 } 48 if nil != event { 49 t.Fatal(event) 50 } 51 } 52 53 func TestLongEventType(t *testing.T) { 54 event, err := CreateCustomEvent(strLen512, map[string]interface{}{"alpha": 1}, now) 55 if err != errEventTypeLength { 56 t.Fatal(err) 57 } 58 if nil != event { 59 t.Fatal(event) 60 } 61 } 62 63 func TestNilParams(t *testing.T) { 64 event, err := CreateCustomEvent("myEvent", nil, now) 65 if nil != err { 66 t.Fatal(err) 67 } 68 js, err := json.Marshal(event) 69 if nil != err { 70 t.Fatal(err) 71 } 72 if string(js) != `[{"type":"myEvent","timestamp":1.41713646e+09},{},{}]` { 73 t.Fatal(string(js)) 74 } 75 } 76 77 func TestMissingEventType(t *testing.T) { 78 event, err := CreateCustomEvent("", map[string]interface{}{"alpha": 1}, now) 79 if err != ErrEventTypeRegex { 80 t.Fatal(err) 81 } 82 if nil != event { 83 t.Fatal(event) 84 } 85 } 86 87 func TestEmptyParams(t *testing.T) { 88 event, err := CreateCustomEvent("myEvent", map[string]interface{}{}, now) 89 if nil != err { 90 t.Fatal(err) 91 } 92 js, err := json.Marshal(event) 93 if nil != err { 94 t.Fatal(err) 95 } 96 if string(js) != `[{"type":"myEvent","timestamp":1.41713646e+09},{},{}]` { 97 t.Fatal(string(js)) 98 } 99 } 100 101 func TestTruncatedStringValue(t *testing.T) { 102 event, err := CreateCustomEvent("myEvent", map[string]interface{}{"alpha": strLen512}, now) 103 if nil != err { 104 t.Fatal(err) 105 } 106 js, err := json.Marshal(event) 107 if nil != err { 108 t.Fatal(err) 109 } 110 if string(js) != `[{"type":"myEvent","timestamp":1.41713646e+09},{"alpha":"`+strLen255+`"},{}]` { 111 t.Fatal(string(js)) 112 } 113 } 114 115 func TestInvalidValueType(t *testing.T) { 116 event, err := CreateCustomEvent("myEvent", map[string]interface{}{"alpha": []string{}}, now) 117 if _, ok := err.(ErrInvalidAttributeType); !ok { 118 t.Fatal(err) 119 } 120 if nil != event { 121 t.Fatal(event) 122 } 123 } 124 125 func TestInvalidCustomAttributeKey(t *testing.T) { 126 event, err := CreateCustomEvent("myEvent", map[string]interface{}{strLen512: 1}, now) 127 if nil == err { 128 t.Fatal(err) 129 } 130 if _, ok := err.(invalidAttributeKeyErr); !ok { 131 t.Fatal(err) 132 } 133 if nil != event { 134 t.Fatal(event) 135 } 136 } 137 138 func TestTooManyAttributes(t *testing.T) { 139 params := make(map[string]interface{}) 140 for i := 0; i < customEventAttributeLimit+1; i++ { 141 params[strconv.Itoa(i)] = i 142 } 143 event, err := CreateCustomEvent("myEvent", params, now) 144 if errNumAttributes != err { 145 t.Fatal(err) 146 } 147 if nil != event { 148 t.Fatal(event) 149 } 150 } 151 152 func TestCustomEventAttributeTypes(t *testing.T) { 153 testcases := []struct { 154 val interface{} 155 js string 156 }{ 157 {"string", `"string"`}, 158 {true, `true`}, 159 {false, `false`}, 160 {nil, `null`}, 161 {uint8(1), `1`}, 162 {uint16(1), `1`}, 163 {uint32(1), `1`}, 164 {uint64(1), `1`}, 165 {int8(1), `1`}, 166 {int16(1), `1`}, 167 {int32(1), `1`}, 168 {int64(1), `1`}, 169 {float32(1), `1`}, 170 {float64(1), `1`}, 171 {uint(1), `1`}, 172 {int(1), `1`}, 173 {uintptr(1), `1`}, 174 } 175 176 for _, tc := range testcases { 177 event, err := CreateCustomEvent("myEvent", map[string]interface{}{"key": tc.val}, now) 178 if nil != err { 179 t.Fatal(err) 180 } 181 js, err := json.Marshal(event) 182 if nil != err { 183 t.Fatal(err) 184 } 185 if string(js) != `[{"type":"myEvent","timestamp":1.41713646e+09},{"key":`+tc.js+`},{}]` { 186 t.Fatal(string(js)) 187 } 188 } 189 } 190 191 func TestCustomParamsCopied(t *testing.T) { 192 params := map[string]interface{}{"alpha": 1} 193 event, err := CreateCustomEvent("myEvent", params, now) 194 if nil != err { 195 t.Fatal(err) 196 } 197 // Attempt to change the params after the event created: 198 params["zip"] = "zap" 199 js, err := json.Marshal(event) 200 if nil != err { 201 t.Fatal(err) 202 } 203 if string(js) != `[{"type":"myEvent","timestamp":1.41713646e+09},{"alpha":1},{}]` { 204 t.Fatal(string(js)) 205 } 206 } 207 208 func TestMultipleAttributeJSON(t *testing.T) { 209 params := map[string]interface{}{"alpha": 1, "beta": 2} 210 event, err := CreateCustomEvent("myEvent", params, now) 211 if nil != err { 212 t.Fatal(err) 213 } 214 js, err := json.Marshal(event) 215 if nil != err { 216 t.Fatal(err) 217 } 218 // Params order may not be deterministic, so we simply test that the 219 // JSON created is valid. 220 var valid interface{} 221 if err := json.Unmarshal(js, &valid); nil != err { 222 t.Error(string(js)) 223 } 224 }