(t *testing.T)
| 60 | } |
| 61 | |
| 62 | func (s) TestConn(t *testing.T) { |
| 63 | defer restoreHooks()() |
| 64 | |
| 65 | // Constant time. |
| 66 | now = func() time.Time { return time.Unix(123, 456) } |
| 67 | |
| 68 | // Capture sleep times for checking later. |
| 69 | var sleepTimes []time.Duration |
| 70 | sleep = func(t time.Duration) { sleepTimes = append(sleepTimes, t) } |
| 71 | |
| 72 | wantSleeps := func(want ...time.Duration) { |
| 73 | if !reflect.DeepEqual(want, sleepTimes) { |
| 74 | t.Fatalf("sleepTimes = %v; want %v", sleepTimes, want) |
| 75 | } |
| 76 | sleepTimes = nil |
| 77 | } |
| 78 | |
| 79 | // Use a fairly high latency to cause a large BDP and avoid sleeps while |
| 80 | // writing due to simulation of full buffers. |
| 81 | latency := 1 * time.Second |
| 82 | c, err := (&Network{Kbps: 1, Latency: latency, MTU: 5}).Conn(bufConn{&bytes.Buffer{}}) |
| 83 | if err != nil { |
| 84 | t.Fatalf("Unexpected error creating connection: %v", err) |
| 85 | } |
| 86 | wantSleeps(latency) // Connection creation delay. |
| 87 | |
| 88 | // 1 kbps = 128 Bps. Divides evenly by 1 second using nanos. |
| 89 | byteLatency := time.Second / 128 |
| 90 | |
| 91 | write := func(b []byte) { |
| 92 | n, err := c.Write(b) |
| 93 | if n != len(b) || err != nil { |
| 94 | t.Fatalf("c.Write(%v) = %v, %v; want %v, nil", b, n, err, len(b)) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | write([]byte{1, 2, 3, 4, 5}) // One full packet |
| 99 | pkt1Time := latency + byteLatency*5 |
| 100 | write([]byte{6}) // One partial packet |
| 101 | pkt2Time := pkt1Time + byteLatency |
| 102 | write([]byte{7, 8, 9, 10, 11, 12, 13}) // Two packets |
| 103 | pkt3Time := pkt2Time + byteLatency*5 |
| 104 | pkt4Time := pkt3Time + byteLatency*2 |
| 105 | |
| 106 | // No reads, so no sleeps yet. |
| 107 | wantSleeps() |
| 108 | |
| 109 | read := func(n int, want []byte) { |
| 110 | b := make([]byte, n) |
| 111 | if rd, err := c.Read(b); err != nil || rd != len(want) { |
| 112 | t.Fatalf("c.Read(<%v bytes>) = %v, %v; want %v, nil", n, rd, err, len(want)) |
| 113 | } |
| 114 | if !reflect.DeepEqual(b[:len(want)], want) { |
| 115 | t.Fatalf("read %v; want %v", b, want) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | read(1, []byte{1}) |
nothing calls this directly
no test coverage detected