Echo writes a message and ensures the same is sent back on c.
(ctx context.Context, c *websocket.Conn, max int)
| 49 | |
| 50 | // Echo writes a message and ensures the same is sent back on c. |
| 51 | func Echo(ctx context.Context, c *websocket.Conn, max int) error { |
| 52 | expType := websocket.MessageBinary |
| 53 | if xrand.Bool() { |
| 54 | expType = websocket.MessageText |
| 55 | } |
| 56 | |
| 57 | msg := randMessage(expType, xrand.Int(max)) |
| 58 | |
| 59 | writeErr := xsync.Go(func() error { |
| 60 | return c.Write(ctx, expType, msg) |
| 61 | }) |
| 62 | |
| 63 | actType, act, err := c.Read(ctx) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | err = <-writeErr |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | if expType != actType { |
| 74 | return fmt.Errorf("unexpected message typ (%v): %v", expType, actType) |
| 75 | } |
| 76 | |
| 77 | if !bytes.Equal(msg, act) { |
| 78 | return fmt.Errorf("unexpected msg read: %#v", act) |
| 79 | } |
| 80 | |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | func randMessage(typ websocket.MessageType, n int) []byte { |
| 85 | if typ == websocket.MessageBinary { |