github.com/status-im/status-go@v1.1.0/protocol/peersyncing/peersyncing_test.go (about) 1 package peersyncing 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/suite" 7 8 "github.com/status-im/status-go/appdatabase" 9 "github.com/status-im/status-go/eth-node/types" 10 "github.com/status-im/status-go/protocol/sqlite" 11 "github.com/status-im/status-go/t/helpers" 12 ) 13 14 func TestPeerSyncingSuite(t *testing.T) { 15 suite.Run(t, new(PeerSyncingSuite)) 16 } 17 18 type PeerSyncingSuite struct { 19 suite.Suite 20 p *PeerSyncing 21 } 22 23 func (s *PeerSyncingSuite) SetupTest() { 24 db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{}) 25 s.Require().NoError(err) 26 27 err = sqlite.Migrate(db) 28 s.Require().NoError(err) 29 30 s.p = New(Config{Database: db}) 31 } 32 33 var testCommunityID = []byte("community-id") 34 35 func (s *PeerSyncingSuite) TestBasic() { 36 37 syncMessage := SyncMessage{ 38 ID: []byte("test-id"), 39 ChatID: testCommunityID, 40 Type: SyncMessageCommunityType, 41 Payload: []byte("test"), 42 Timestamp: 1, 43 } 44 45 s.Require().NoError(s.p.Add(syncMessage)) 46 47 allMessages, err := s.p.AvailableMessages() 48 49 s.Require().NoError(err) 50 s.Require().Len(allMessages, 1) 51 52 byChatID, err := s.p.AvailableMessagesMapByChatIDs([][]byte{syncMessage.ChatID}, 10) 53 54 s.Require().NoError(err) 55 s.Require().Len(byChatID, 1) 56 57 byChatID, err = s.p.AvailableMessagesMapByChatIDs([][]byte{[]byte("random-group-id")}, 10) 58 59 s.Require().NoError(err) 60 s.Require().Len(byChatID, 0) 61 62 newSyncMessage := SyncMessage{ 63 ID: []byte("test-id-2"), 64 ChatID: testCommunityID, 65 Type: SyncMessageCommunityType, 66 Payload: []byte("test-2"), 67 Timestamp: 2, 68 } 69 70 wantedMessages, err := s.p.OnOffer([]SyncMessage{syncMessage, newSyncMessage}) 71 s.Require().NoError(err) 72 73 s.Require().Len(wantedMessages, 1) 74 s.Require().Equal(newSyncMessage.ID, wantedMessages[0].ID) 75 } 76 77 func (s *PeerSyncingSuite) TestOrderAndLimit() { 78 79 syncMessage1 := SyncMessage{ 80 ID: []byte("test-id-1"), 81 ChatID: testCommunityID, 82 Type: SyncMessageCommunityType, 83 Payload: []byte("test"), 84 Timestamp: 1, 85 } 86 87 syncMessage2 := SyncMessage{ 88 ID: []byte("test-id-2"), 89 ChatID: testCommunityID, 90 Type: SyncMessageCommunityType, 91 Payload: []byte("test"), 92 Timestamp: 2, 93 } 94 95 syncMessage3 := SyncMessage{ 96 ID: []byte("test-id-3"), 97 ChatID: testCommunityID, 98 Type: SyncMessageCommunityType, 99 Payload: []byte("test"), 100 Timestamp: 3, 101 } 102 103 syncMessage4 := SyncMessage{ 104 ID: []byte("test-id-4"), 105 ChatID: testCommunityID, 106 Type: SyncMessageCommunityType, 107 Payload: []byte("test"), 108 Timestamp: 4, 109 } 110 111 s.Require().NoError(s.p.Add(syncMessage1)) 112 s.Require().NoError(s.p.Add(syncMessage2)) 113 s.Require().NoError(s.p.Add(syncMessage3)) 114 s.Require().NoError(s.p.Add(syncMessage4)) 115 116 byChatID, err := s.p.AvailableMessagesMapByChatIDs([][]byte{testCommunityID}, 10) 117 118 s.Require().NoError(err) 119 s.Require().Len(byChatID, 1) 120 s.Require().Len(byChatID[types.Bytes2Hex(testCommunityID)], 4) 121 122 byChatID, err = s.p.AvailableMessagesMapByChatIDs([][]byte{testCommunityID}, 3) 123 124 s.Require().NoError(err) 125 s.Require().Len(byChatID, 1) 126 s.Require().Len(byChatID[types.Bytes2Hex(testCommunityID)], 3) 127 }