github.com/status-im/status-go@v1.1.0/services/ext/requests_test.go (about) 1 package ext 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/status-im/status-go/eth-node/types" 10 ) 11 12 func TestRegisterSameRequests(t *testing.T) { 13 registry := NewRequestsRegistry(10 * time.Second) 14 topics := []types.TopicType{{1}} 15 require.NoError(t, registry.Register(types.Hash{1}, topics)) 16 require.Error(t, registry.Register(types.Hash{2}, topics)) 17 } 18 19 func TestRegisterSameRequestsWithoutDelay(t *testing.T) { 20 registry := NewRequestsRegistry(0) 21 topics := []types.TopicType{{1}} 22 require.NoError(t, registry.Register(types.Hash{1}, topics)) 23 require.NoError(t, registry.Register(types.Hash{2}, topics)) 24 } 25 26 func TestRegisterDifferentRequests(t *testing.T) { 27 registry := NewRequestsRegistry(10 * time.Second) 28 require.NoError(t, registry.Register(types.Hash{1}, []types.TopicType{{1}})) 29 require.NoError(t, registry.Register(types.Hash{2}, []types.TopicType{{2}})) 30 } 31 32 func TestUnregisterReplacedRequest(t *testing.T) { 33 registry := NewRequestsRegistry(0) 34 unreg := types.Hash{1} 35 topics := []types.TopicType{{1}} 36 require.NoError(t, registry.Register(unreg, topics)) 37 replacement := types.Hash{2} 38 require.NoError(t, registry.Register(replacement, topics)) 39 // record should be replaced with types.Hash{2}, so when we will remove unreg it will not affect topics map 40 registry.Unregister(unreg) 41 record, exist := registry.uidToTopics[replacement] 42 require.True(t, exist, "replaced record should exist") 43 require.Equal(t, replacement, registry.byTopicsHash[record].lastUID) 44 }