status-go/services/ext/requests_test.go

45 lines
1.5 KiB
Go
Raw Normal View History

package ext
import (
"testing"
"time"
"github.com/stretchr/testify/require"
2020-01-02 09:10:19 +00:00
"github.com/status-im/status-go/eth-node/types"
)
func TestRegisterSameRequests(t *testing.T) {
registry := NewRequestsRegistry(10 * time.Second)
topics := []types.TopicType{{1}}
require.NoError(t, registry.Register(types.Hash{1}, topics))
require.Error(t, registry.Register(types.Hash{2}, topics))
}
func TestRegisterSameRequestsWithoutDelay(t *testing.T) {
registry := NewRequestsRegistry(0)
topics := []types.TopicType{{1}}
require.NoError(t, registry.Register(types.Hash{1}, topics))
require.NoError(t, registry.Register(types.Hash{2}, topics))
}
func TestRegisterDifferentRequests(t *testing.T) {
registry := NewRequestsRegistry(10 * time.Second)
require.NoError(t, registry.Register(types.Hash{1}, []types.TopicType{{1}}))
require.NoError(t, registry.Register(types.Hash{2}, []types.TopicType{{2}}))
}
func TestUnregisterReplacedRequest(t *testing.T) {
registry := NewRequestsRegistry(0)
unreg := types.Hash{1}
topics := []types.TopicType{{1}}
require.NoError(t, registry.Register(unreg, topics))
replacement := types.Hash{2}
require.NoError(t, registry.Register(replacement, topics))
// record should be replaced with types.Hash{2}, so when we will remove unreg it will not affect topics map
registry.Unregister(unreg)
record, exist := registry.uidToTopics[replacement]
require.True(t, exist, "replaced record should exist")
require.Equal(t, replacement, registry.byTopicsHash[record].lastUID)
}