Skip to content

Commit

Permalink
ARCO-204: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Sep 24, 2024
1 parent 910910f commit 097ef43
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
30 changes: 0 additions & 30 deletions internal/cache/mock_store.go

This file was deleted.

9 changes: 7 additions & 2 deletions internal/metamorph/health_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metamorph_test
import (
"context"
"errors"
"github.com/bitcoin-sv/arc/internal/cache"
"testing"

"github.com/bitcoin-sv/arc/internal/metamorph"
Expand All @@ -13,6 +14,8 @@ import (
)

func TestCheck(t *testing.T) {
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)

tt := []struct {
name string
service string
Expand Down Expand Up @@ -68,7 +71,7 @@ func TestCheck(t *testing.T) {
},
}

sut := metamorph.NewServer(metamorphStore, processor)
sut := metamorph.NewServer(metamorphStore, processor, cacheStore)

// when
resp, err := sut.Check(context.Background(), req)
Expand All @@ -81,6 +84,8 @@ func TestCheck(t *testing.T) {
}

func TestWatch(t *testing.T) {
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)

tt := []struct {
name string
service string
Expand Down Expand Up @@ -136,7 +141,7 @@ func TestWatch(t *testing.T) {
},
}

sut := metamorph.NewServer(metamorphStore, processor)
sut := metamorph.NewServer(metamorphStore, processor, cacheStore)

watchServer := &mocks.HealthWatchServerMock{
SendFunc: func(healthCheckResponse *grpc_health_v1.HealthCheckResponse) error {
Expand Down
30 changes: 22 additions & 8 deletions internal/metamorph/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/bitcoin-sv/arc/internal/cache"
"log/slog"
"os"
"testing"
Expand All @@ -27,21 +28,23 @@ import (

func TestNewServer(t *testing.T) {
t.Run("NewServer", func(t *testing.T) {
server := metamorph.NewServer(nil, nil)
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)
server := metamorph.NewServer(nil, nil, cacheStore)
assert.IsType(t, &metamorph.Server{}, server)
})
}

func TestHealth(t *testing.T) {
t.Run("Health", func(t *testing.T) {
// given
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)
processor := &mocks.ProcessorIMock{}
processor.GetProcessorMapSizeFunc = func() int { return 22 }
processor.GetPeersFunc = func() []p2p.PeerI {
return []p2p.PeerI{}
}

sut := metamorph.NewServer(nil, processor)
sut := metamorph.NewServer(nil, processor, cacheStore)

// when
stats, err := sut.Health(context.Background(), &emptypb.Empty{})
Expand All @@ -53,6 +56,8 @@ func TestHealth(t *testing.T) {
}

func TestPutTransaction(t *testing.T) {
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)

testCases := []struct {
name string
processorResponse metamorph.StatusAndError
Expand Down Expand Up @@ -125,7 +130,7 @@ func TestPutTransaction(t *testing.T) {
},
}

sut := metamorph.NewServer(s, processor, metamorph.WithMaxTimeoutDefault(100*time.Millisecond))
sut := metamorph.NewServer(s, processor, cacheStore, metamorph.WithMaxTimeoutDefault(100*time.Millisecond))

txRequest := &metamorph_api.TransactionRequest{
RawTx: testdata.TX1Raw.Bytes(),
Expand All @@ -150,6 +155,7 @@ func TestPutTransaction(t *testing.T) {

func TestServer_GetTransactionStatus(t *testing.T) {
errFailedToGetTxData := errors.New("failed to get transaction data")
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)

tests := []struct {
name string
Expand Down Expand Up @@ -286,7 +292,7 @@ func TestServer_GetTransactionStatus(t *testing.T) {
},
}

sut := metamorph.NewServer(metamorphStore, nil)
sut := metamorph.NewServer(metamorphStore, nil, cacheStore)

// when
got, err := sut.GetTransactionStatus(context.Background(), tt.req)
Expand All @@ -301,6 +307,8 @@ func TestServer_GetTransactionStatus(t *testing.T) {
}

func TestPutTransactions(t *testing.T) {
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)

hash0, err := chainhash.NewHashFromStr("9b58926ec7eed21ec2f3ca518d5fc0c6ccbf963e25c3e7ac496c99867d97599a")
require.NoError(t, err)

Expand Down Expand Up @@ -548,7 +556,7 @@ func TestPutTransactions(t *testing.T) {
}

serverLogger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
sut := metamorph.NewServer(nil, processor, metamorph.WithLogger(serverLogger), metamorph.WithMaxTimeoutDefault(5*time.Second))
sut := metamorph.NewServer(nil, processor, cacheStore, metamorph.WithLogger(serverLogger), metamorph.WithMaxTimeoutDefault(5*time.Second))

// when
statuses, err := sut.PutTransactions(context.Background(), tc.requests)
Expand All @@ -572,6 +580,8 @@ func TestPutTransactions(t *testing.T) {
}

func TestSetUnlockedByName(t *testing.T) {
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)

tt := []struct {
name string
recordsAffected int64
Expand Down Expand Up @@ -606,7 +616,7 @@ func TestSetUnlockedByName(t *testing.T) {
},
}

sut := metamorph.NewServer(metamorphStore, nil)
sut := metamorph.NewServer(metamorphStore, nil, cacheStore)

// when
response, err := sut.SetUnlockedByName(context.Background(), &metamorph_api.SetUnlockedByNameRequest{
Expand All @@ -627,6 +637,8 @@ func TestSetUnlockedByName(t *testing.T) {
}

func TestStartGRPCServer(t *testing.T) {
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)

tt := []struct {
name string
}{
Expand All @@ -648,7 +660,7 @@ func TestStartGRPCServer(t *testing.T) {
processor := &mocks.ProcessorIMock{
ShutdownFunc: func() {},
}
sut := metamorph.NewServer(metamorphStore, processor)
sut := metamorph.NewServer(metamorphStore, processor, cacheStore)
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))

// when
Expand All @@ -663,6 +675,8 @@ func TestStartGRPCServer(t *testing.T) {
}

func TestGetTransactions(t *testing.T) {
cacheStore := cache.NewFreecacheStore(100 * 1024 * 1024)

tcs := []struct {
name string
request *metamorph_api.TransactionsStatusRequest
Expand Down Expand Up @@ -725,7 +739,7 @@ func TestGetTransactions(t *testing.T) {
},
}

sut := metamorph.NewServer(&store, nil)
sut := metamorph.NewServer(&store, nil, cacheStore)

// when
res, err := sut.GetTransactions(context.TODO(), tc.request)
Expand Down
7 changes: 7 additions & 0 deletions test/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ peers:
p2p: 18333
zmq: 28332

cache:
engine: redis
redis:
addr: arc-redis:6379
password: ""
db: 1

metamorph:
listenAddr: 0.0.0.0:8001
dialAddr: arc-metamorph:8001
Expand Down

0 comments on commit 097ef43

Please sign in to comment.