| 115 | } |
| 116 | |
| 117 | func TestSyncHeartbeats(t *testing.T) { |
| 118 | ln, client, server, dispatcher, err := newSyncTest() |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | defer ln.Close() |
| 123 | |
| 124 | server.heartbeatInterval = 500 * time.Millisecond |
| 125 | go server.run() |
| 126 | go client.run() |
| 127 | |
| 128 | // Enabling briefly shouldn't have time to get a heartbeat, but should get |
| 129 | // the initial desync. |
| 130 | client.enable() |
| 131 | // Make sure we can read the desync (and flush it so the later client doesn't |
| 132 | // see it). |
| 133 | if n, err := dispatcher.nextNote(); err != nil || n.Type != SNTDesync { |
| 134 | t.Errorf("During short enablement, nextNote() = %v, %v; expected desync", n, err) |
| 135 | } else { |
| 136 | t.Logf("Got short note: %v, %v", n, err) |
| 137 | } |
| 138 | client.disable() |
| 139 | |
| 140 | // Now let it run long enough to get some heartbeats. |
| 141 | // Any heartbeats sent to the first enablement of the client shouldn't appear here. |
| 142 | client.enable() |
| 143 | time.Sleep(2*server.heartbeatInterval + 50*time.Millisecond) |
| 144 | client.disable() |
| 145 | // Waiting longer after disabling shouldn't send another heartbeat (and |
| 146 | // should make sure we receive the ones we had queued). |
| 147 | time.Sleep(server.heartbeatInterval + 50*time.Millisecond) |
| 148 | |
| 149 | wantNotes := []SyncNoteType{SNTDesync, SNTHeartbeat, SNTHeartbeat} |
| 150 | for _, nt := range wantNotes { |
| 151 | n, err := dispatcher.nextNote() |
| 152 | if err != nil || n.Type != nt { |
| 153 | t.Errorf("After long enablement, nextNote() = %v, %v; want Type = %v", n, err, nt) |
| 154 | continue |
| 155 | } |
| 156 | t.Logf("After long enablement: got sync note %v", n) |
| 157 | } |
| 158 | |
| 159 | n, err := dispatcher.nextNote() |
| 160 | if err != nil { |
| 161 | // TODO: Confirm it's a timeout? |
| 162 | t.Logf("After flushing expected notes, final nextNote read failed expectedly: %v", err) |
| 163 | return |
| 164 | } |
| 165 | // If we end up registering near the heartbeat boundaries, we might |
| 166 | // legitimately get 3 heartbeats, but no more! |
| 167 | if n.Type != SNTHeartbeat { |
| 168 | t.Errorf("After long enablement, got additional note %v, expected only %d notes", n, len(wantNotes)) |
| 169 | } else { |
| 170 | n, err := dispatcher.nextNote() |
| 171 | if err == nil { |
| 172 | t.Errorf("After long enablement, got additional note %v, expected at most %d notes", n, len(wantNotes)+1) |
| 173 | } |
| 174 | } |