FakeRPC returns a blocking async function which simulates an RPC that succeeds after a short delay, or fails if the provided per-call timeout elapses first.
(timeout time.Duration)
| 25 | // FakeRPC returns a blocking async function which simulates an RPC that succeeds after a short |
| 26 | // delay, or fails if the provided per-call timeout elapses first. |
| 27 | func FakeRPC(timeout time.Duration) func(context.Context, ...ref.Val) ref.Val { |
| 28 | return func(ctx context.Context, args ...ref.Val) ref.Val { |
| 29 | rpcCtx, cancel := context.WithTimeout(ctx, timeout) |
| 30 | defer cancel() |
| 31 | select { |
| 32 | case <-time.After(20 * time.Millisecond): |
| 33 | in := args[0].(types.String) |
| 34 | return in.Add(types.String(" success!")) |
| 35 | case <-rpcCtx.Done(): |
| 36 | return types.NewErr(rpcCtx.Err().Error()) |
| 37 | } |
| 38 | } |
| 39 | } |