2020-01-20 20:56:06 +00:00
|
|
|
package ext
|
2019-01-15 09:21:33 +00:00
|
|
|
|
|
|
|
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"
|
2019-01-15 09:21:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRegisterSameRequests(t *testing.T) {
|
|
|
|
registry := NewRequestsRegistry(10 * time.Second)
|
2019-11-23 17:57:05 +00:00
|
|
|
topics := []types.TopicType{{1}}
|
|
|
|
require.NoError(t, registry.Register(types.Hash{1}, topics))
|
|
|
|
require.Error(t, registry.Register(types.Hash{2}, topics))
|
2019-01-15 09:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterSameRequestsWithoutDelay(t *testing.T) {
|
|
|
|
registry := NewRequestsRegistry(0)
|
2019-11-23 17:57:05 +00:00
|
|
|
topics := []types.TopicType{{1}}
|
|
|
|
require.NoError(t, registry.Register(types.Hash{1}, topics))
|
|
|
|
require.NoError(t, registry.Register(types.Hash{2}, topics))
|
2019-01-15 09:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterDifferentRequests(t *testing.T) {
|
|
|
|
registry := NewRequestsRegistry(10 * time.Second)
|
2019-11-23 17:57:05 +00:00
|
|
|
require.NoError(t, registry.Register(types.Hash{1}, []types.TopicType{{1}}))
|
|
|
|
require.NoError(t, registry.Register(types.Hash{2}, []types.TopicType{{2}}))
|
2019-01-15 09:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnregisterReplacedRequest(t *testing.T) {
|
|
|
|
registry := NewRequestsRegistry(0)
|
2019-11-23 17:57:05 +00:00
|
|
|
unreg := types.Hash{1}
|
|
|
|
topics := []types.TopicType{{1}}
|
2019-01-15 09:21:33 +00:00
|
|
|
require.NoError(t, registry.Register(unreg, topics))
|
2019-11-23 17:57:05 +00:00
|
|
|
replacement := types.Hash{2}
|
2019-01-15 09:21:33 +00:00
|
|
|
require.NoError(t, registry.Register(replacement, topics))
|
2019-11-23 17:57:05 +00:00
|
|
|
// record should be replaced with types.Hash{2}, so when we will remove unreg it will not affect topics map
|
2019-01-15 09:21:33 +00:00
|
|
|
registry.Unregister(unreg)
|
|
|
|
record, exist := registry.uidToTopics[replacement]
|
|
|
|
require.True(t, exist, "replaced record should exist")
|
|
|
|
require.Equal(t, replacement, registry.byTopicsHash[record].lastUID)
|
|
|
|
}
|