github.com/gobwas/gtrace@v0.4.3/test/test_test.go (about) 1 package test 2 3 import ( 4 "context" 5 "reflect" 6 "testing" 7 ) 8 9 func TestReturnedTrace(t *testing.T) { 10 var called bool 11 trace := TraceReturningTrace{ 12 OnReturnedTrace: func() ReturnedTrace { 13 return ReturnedTrace{ 14 OnSomething: func(a, b int) { 15 called = true 16 }, 17 } 18 }, 19 } 20 trace.onReturnedTrace().onSomething(1, 2) 21 if !called { 22 t.Fatalf("not called") 23 } 24 } 25 26 func TestConditional(t *testing.T) { 27 var called bool 28 trace := ConditionalBuildTrace{ 29 OnSomething: func() { 30 called = true 31 }, 32 } 33 trace.onSomething() 34 if !called { 35 t.Fatalf("not called") 36 } 37 } 38 39 func TestCompose(t *testing.T) { 40 var act []string 41 called := func(name, arg string) { 42 act = append(act, name+":"+arg) 43 } 44 for _, test := range []struct { 45 name string 46 a Trace 47 b Trace 48 exp []string 49 }{ 50 { 51 a: Trace{ 52 OnTest: func(s string) func(string) { 53 called("a", s) 54 return nil 55 }, 56 }, 57 b: Trace{}, 58 59 exp: []string{ 60 "a:start", 61 }, 62 }, 63 { 64 a: Trace{}, 65 b: Trace{ 66 OnTest: func(s string) func(string) { 67 called("b", s) 68 return nil 69 }, 70 }, 71 exp: []string{ 72 "b:start", 73 }, 74 }, 75 { 76 a: Trace{ 77 OnTest: func(s string) func(string) { 78 called("a", s) 79 return nil 80 }, 81 }, 82 b: Trace{ 83 OnTest: func(s string) func(string) { 84 called("b", s) 85 return nil 86 }, 87 }, 88 exp: []string{ 89 "a:start", 90 "b:start", 91 }, 92 }, 93 { 94 a: Trace{ 95 OnTest: func(s string) func(string) { 96 called("a", s) 97 return func(s string) { 98 called("a", s) 99 } 100 }, 101 }, 102 b: Trace{}, 103 104 exp: []string{ 105 "a:start", 106 "a:done", 107 }, 108 }, 109 { 110 a: Trace{}, 111 b: Trace{ 112 OnTest: func(s string) func(string) { 113 called("b", s) 114 return func(s string) { 115 called("b", s) 116 } 117 }, 118 }, 119 exp: []string{ 120 "b:start", 121 "b:done", 122 }, 123 }, 124 { 125 a: Trace{ 126 OnTest: func(s string) func(string) { 127 called("a", s) 128 return func(s string) { 129 called("a", s) 130 } 131 }, 132 }, 133 b: Trace{ 134 OnTest: func(s string) func(string) { 135 called("b", s) 136 return func(s string) { 137 called("b", s) 138 } 139 }, 140 }, 141 exp: []string{ 142 "a:start", 143 "b:start", 144 "a:done", 145 "b:done", 146 }, 147 }, 148 { 149 a: Trace{ 150 OnTest: func(s string) func(string) { 151 called("a", s) 152 return func(s string) { 153 called("a", s) 154 } 155 }, 156 }, 157 b: Trace{ 158 OnTest: func(s string) func(string) { 159 called("b", s) 160 return nil 161 }, 162 }, 163 exp: []string{ 164 "a:start", 165 "b:start", 166 "a:done", 167 }, 168 }, 169 { 170 a: Trace{ 171 OnTest: func(s string) func(string) { 172 called("a", s) 173 return nil 174 }, 175 }, 176 b: Trace{ 177 OnTest: func(s string) func(string) { 178 called("b", s) 179 return func(s string) { 180 called("b", s) 181 } 182 }, 183 }, 184 exp: []string{ 185 "a:start", 186 "b:start", 187 "b:done", 188 }, 189 }, 190 } { 191 t.Run(test.name, func(t *testing.T) { 192 t.Cleanup(func() { 193 act = nil 194 }) 195 c := test.a.Compose(test.b) 196 done := c.onTest(context.Background(), "start") 197 done("done") 198 199 if exp := test.exp; !reflect.DeepEqual(act, exp) { 200 t.Fatalf("unexpected calls: %v; want %v", act, exp) 201 } 202 }) 203 } 204 } 205 206 func TestShortcutPerFieldTrace(t *testing.T) { 207 var called bool 208 t0 := ShortcutPerFieldTrace{ 209 OnFoo: func() { 210 called = true 211 }, 212 } 213 shortcutPerFieldTraceOnFoo(t0) 214 if !called { 215 t.Fatalf("hook wasn't called") 216 } 217 } 218 219 func TestBuildTagTrace(t *testing.T) { 220 t0 := BuildTagTrace{ 221 OnSomethingA: func() func() { 222 panic("must not be called") 223 }, 224 OnSomethingB: func(int8, int16) func(int32, int64) { 225 panic("must not be called") 226 }, 227 OnSomethingC: func(Type) func(Type) { 228 panic("must not be called") 229 }, 230 } 231 t1 := BuildTagTrace{ 232 OnSomethingA: func() func() { 233 panic("must not be called") 234 }, 235 OnSomethingB: func(int8, int16) func(int32, int64) { 236 panic("must not be called") 237 }, 238 OnSomethingC: func(Type) func(Type) { 239 panic("must not be called") 240 }, 241 } 242 trace := t0.Compose(t1) 243 trace.onSomethingA(context.Background())() 244 trace.onSomethingB(context.Background(), 1, 2)(3, 4) 245 trace.onSomethingC(context.Background(), Type{})(Type{}) 246 } 247 248 func BenchmarkBuildTagTrace(b *testing.B) { 249 x := BuildTagTrace{ 250 OnSomethingA: func() func() { 251 return func() { 252 // 253 } 254 }, 255 OnSomethingB: func(int8, int16) func(int32, int64) { 256 return func(int32, int64) { 257 // 258 } 259 }, 260 OnSomethingC: func(Type) func(Type) { 261 return func(Type) { 262 // 263 } 264 }, 265 } 266 267 t := x.Compose(x).Compose(x).Compose(x) 268 269 b.Run("OnSomethingA", func(b *testing.B) { 270 for i := 0; i < b.N; i++ { 271 t.onSomethingA(context.Background())() 272 } 273 }) 274 b.Run("OnSomethingB", func(b *testing.B) { 275 for i := 0; i < b.N; i++ { 276 t.onSomethingB(context.Background(), 1, 2)(3, 4) 277 } 278 }) 279 b.Run("OnSomethingC", func(b *testing.B) { 280 for i := 0; i < b.N; i++ { 281 t.onSomethingC(context.Background(), Type{})(Type{}) 282 } 283 }) 284 }