(t *testing.T)
| 276 | } |
| 277 | |
| 278 | func TestNoConcurrentCallbacks(t *testing.T) { |
| 279 | const timeout = 2 * time.Second |
| 280 | |
| 281 | type Callback struct { |
| 282 | Index int |
| 283 | Func dnode.Function |
| 284 | } |
| 285 | |
| 286 | k := newXhrKite("callback", "0.0.1") |
| 287 | k.Config.DisableAuthentication = true |
| 288 | k.HandleFunc("call", func(r *Request) (interface{}, error) { |
| 289 | if r.Args == nil { |
| 290 | return nil, errors.New("empty argument") |
| 291 | } |
| 292 | |
| 293 | var arg Callback |
| 294 | if err := r.Args.One().Unmarshal(&arg); err != nil { |
| 295 | return nil, err |
| 296 | } |
| 297 | |
| 298 | if !arg.Func.IsValid() { |
| 299 | return nil, errors.New("invalid argument") |
| 300 | } |
| 301 | |
| 302 | if err := arg.Func.Call(arg.Index); err != nil { |
| 303 | return nil, err |
| 304 | } |
| 305 | |
| 306 | return true, nil |
| 307 | }) |
| 308 | |
| 309 | go k.Run() |
| 310 | <-k.ServerReadyNotify() |
| 311 | defer k.Close() |
| 312 | |
| 313 | url := fmt.Sprintf("http://127.0.0.1:%d/kite", k.Port()) |
| 314 | |
| 315 | c := k.NewClient(url) |
| 316 | defer c.Close() |
| 317 | |
| 318 | // The TestNoConcurrentCallbacks asserts ConcurrentCallbacks |
| 319 | // are disabled by default for each new client. |
| 320 | // |
| 321 | // When callbacks are executed concurrently, the order |
| 322 | // of indices received on the channel is random, |
| 323 | // thus making this test to fail. |
| 324 | // |
| 325 | // c.ConcurrentCallbacks = true |
| 326 | |
| 327 | if err := c.DialTimeout(timeout); err != nil { |
| 328 | t.Errorf("DialTimeout(%q)=%s", url, err) |
| 329 | } |
| 330 | |
| 331 | indices := make(chan int, 50) |
| 332 | callback := dnode.Callback(func(arg *dnode.Partial) { |
| 333 | var index int |
| 334 | if err := arg.One().Unmarshal(&index); err != nil { |
| 335 | t.Logf("failed to unmarshal: %s", err) |
nothing calls this directly
no test coverage detected