package routewatch import ( "testing" "git.eeqj.de/sneak/routewatch/internal/database" "git.eeqj.de/sneak/routewatch/internal/logger" ) // peerBatchStore keeps the peers of the last UpdatePeerBatch call. The // database has no read of the peer table, so the peer handler's test checks // what the handler writes. type peerBatchStore struct { *mockStore peers map[string]database.PeerUpdate } func (s *peerBatchStore) UpdatePeerBatch( peers map[string]database.PeerUpdate, ) error { s.peers = peers return nil } // TestPeerHandlerWritesTheEarliestAndLatestTimesOfANewPeer handles two updates // from one peer, the second 5 seconds after the first, in one batch. The peer // handler writes the peer first seen at the time of the first update and last // seen at the time of the second. func TestPeerHandlerWritesTheEarliestAndLatestTimesOfANewPeer(t *testing.T) { t.Parallel() store := &peerBatchStore{mockStore: newMockStore()} // Built without NewPeerHandler's flush goroutine, so the test flushes // itself. h := &PeerHandler{db: store, logger: logger.New(), batchMaxSize: peerBatchMaxSize} first := peerAnnouncement(testPeerIP, testV4Prefix) second := peerAnnouncement(testPeerIP, testV4Prefix) second.ParsedTimestamp = first.ParsedTimestamp.Add(secondUpdateAfter) h.HandleMessage(first) h.HandleMessage(second) h.flushBatch() seen := store.peers[testPeerIP].Seen if !seen.First.Equal(first.ParsedTimestamp) { t.Errorf("first_seen of peer %s = %s, want %s", testPeerIP, seen.First, first.ParsedTimestamp) } if !seen.Last.Equal(second.ParsedTimestamp) { t.Errorf("last_seen of peer %s = %s, want %s", testPeerIP, seen.Last, second.ParsedTimestamp) } }