discovery: use T.TempDir to create temporary test directory

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun committed 2022-08-24 08:58:16 +08:00
1 parent d1541d6628
commit dbf3cf62fe
2 files changed
+47 -95

No files matched your search

+8 -18
View File
@@ -2,9 +2,7 @@ package discovery
import (
"bytes"
"io/ioutil"
"math/rand"
"os"
"reflect"
"testing"
@@ -16,29 +14,24 @@ import (
"github.com/stretchr/testify/require"
)
func createTestMessageStore(t *testing.T) (*MessageStore, func()) {
func createTestMessageStore(t *testing.T) *MessageStore {
t.Helper()
tempDir, err := ioutil.TempDir("", "channeldb")
require.NoError(t, err, "unable to create temp dir")
db, err := channeldb.Open(tempDir)
db, err := channeldb.Open(t.TempDir())
if err != nil {
os.RemoveAll(tempDir)
t.Fatalf("unable to open db: %v", err)
}
cleanUp := func() {
t.Cleanup(func() {
db.Close()
os.RemoveAll(tempDir)
}
})
store, err := NewMessageStore(db)
if err != nil {
cleanUp()
t.Fatalf("unable to initialize message store: %v", err)
}
return store, cleanUp
return store
}
func randPubKey(t *testing.T) *btcec.PublicKey {
@@ -79,8 +72,7 @@ func TestMessageStoreMessages(t *testing.T) {
t.Parallel()
// We'll start by creating our test message store.
msgStore, cleanUp := createTestMessageStore(t)
defer cleanUp()
msgStore := createTestMessageStore(t)
// We'll then create some test messages for two test peers, and none for
// an additional test peer.
@@ -212,8 +204,7 @@ func TestMessageStoreUnsupportedMessage(t *testing.T) {
t.Parallel()
// We'll start by creating our test message store.
msgStore, cleanUp := createTestMessageStore(t)
defer cleanUp()
msgStore := createTestMessageStore(t)
// Create a message that is known to not be supported by the store.
peer := randCompressedPubKey(t)
@@ -261,8 +252,7 @@ func TestMessageStoreUnsupportedMessage(t *testing.T) {
func TestMessageStoreDeleteMessage(t *testing.T) {
t.Parallel()
msgStore, cleanUp := createTestMessageStore(t)
defer cleanUp()
msgStore := createTestMessageStore(t)
// assertMsg is a helper closure we'll use to ensure a message
// does/doesn't exist within the store.