(t *testing.T)
| 1373 | } |
| 1374 | |
| 1375 | func TestAsyncBinding(t *testing.T) { |
| 1376 | fn, err := NewFunction("async_fn", |
| 1377 | Overload("async_fn_int", []*types.Type{types.IntType}, types.IntType, |
| 1378 | AsyncBinding(func(ctx context.Context, args ...ref.Val) ref.Val { |
| 1379 | return args[0] |
| 1380 | }), |
| 1381 | ), |
| 1382 | ) |
| 1383 | if err != nil { |
| 1384 | t.Fatalf("NewFunction() failed: %v", err) |
| 1385 | } |
| 1386 | for _, od := range fn.OverloadDecls() { |
| 1387 | if !od.HasBinding() { |
| 1388 | t.Errorf("Overload %s does not have binding, wanted async binding", od.ID()) |
| 1389 | } |
| 1390 | if !od.HasLateBinding() { |
| 1391 | t.Errorf("Overload %s does not have late binding, wanted async binding to be late bound", od.ID()) |
| 1392 | } |
| 1393 | } |
| 1394 | if !fn.HasLateBinding() { |
| 1395 | t.Errorf("Function %s does not have late binding, wanted async function to be late bound", fn.Name()) |
| 1396 | } |
| 1397 | bindings, err := fn.Bindings() |
| 1398 | if err != nil { |
| 1399 | t.Fatalf("fn.Bindings() produced an err: %v", err) |
| 1400 | } |
| 1401 | if len(bindings) != 2 { |
| 1402 | t.Errorf("fn.Bindings() produced %d bindings, wanted 2", len(bindings)) |
| 1403 | } |
| 1404 | for _, binding := range bindings { |
| 1405 | if binding.Async == nil { |
| 1406 | t.Fatal("binding missing Async implementation") |
| 1407 | } |
| 1408 | |
| 1409 | ctx := context.Background() |
| 1410 | ch := binding.Async(ctx, types.Int(42)) |
| 1411 | select { |
| 1412 | case res := <-ch: |
| 1413 | if res.Equal(types.Int(42)) != types.True { |
| 1414 | t.Errorf("async binding returned %v, wanted 42", res) |
| 1415 | } |
| 1416 | case <-time.After(1 * time.Second): |
| 1417 | t.Fatal("async binding timed out") |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | func TestAsyncBindingTypeGuards(t *testing.T) { |
| 1423 | fn, err := NewFunction("async_fn", |
nothing calls this directly
no test coverage detected