mirror of
https://github.com/status-im/status-go.git
synced 2025-01-09 06:12:55 +00:00
83ad76637a
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `os.MkdirTemp` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package mailserver
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
waku "github.com/status-im/status-go/waku/common"
|
|
)
|
|
|
|
func TestLevelDB_BuildIteratorWithTopic(t *testing.T) {
|
|
topic := []byte{0x01, 0x02, 0x03, 0x04}
|
|
|
|
db, err := NewLevelDB(t.TempDir())
|
|
require.NoError(t, err)
|
|
|
|
envelope, err := newTestEnvelope(topic)
|
|
require.NoError(t, err)
|
|
err = db.SaveEnvelope(envelope)
|
|
require.NoError(t, err)
|
|
|
|
iter, err := db.BuildIterator(CursorQuery{
|
|
start: NewDBKey(uint32(time.Now().Add(-time.Hour).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
|
|
end: NewDBKey(uint32(time.Now().Add(time.Second).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
|
|
topics: [][]byte{topic},
|
|
limit: 10,
|
|
})
|
|
topicsMap := make(map[types.TopicType]bool)
|
|
topicsMap[types.BytesToTopic(topic)] = true
|
|
require.NoError(t, err)
|
|
hasNext := iter.Next()
|
|
require.True(t, hasNext)
|
|
rawValue, err := iter.GetEnvelopeByTopicsMap(topicsMap)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, rawValue)
|
|
var receivedEnvelope waku.Envelope
|
|
err = rlp.DecodeBytes(rawValue, &receivedEnvelope)
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, waku.BytesToTopic(topic), receivedEnvelope.Topic)
|
|
|
|
err = iter.Release()
|
|
require.NoError(t, err)
|
|
require.NoError(t, iter.Error())
|
|
}
|