Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aloknerurkar committed Jun 14, 2021
1 parent bccfba9 commit b37978c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/postage/batchservice/batchservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (svc *batchService) Create(id, owner []byte, normalisedBalance *big.Int, de
return fmt.Errorf("put: %w", err)
}

if bytes.Equal(svc.owner, owner) {
if bytes.Equal(svc.owner, owner) && svc.batchListener != nil {
svc.batchListener.Handle(b)
}

Expand Down
41 changes: 35 additions & 6 deletions pkg/postage/batchservice/batchservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ func newMockListener() *mockListener {
return &mockListener{}
}

type mockBatchCreationHandler struct {
count int
}

func (m *mockBatchCreationHandler) Handle(b *postage.Batch) {
m.count++
}

func TestBatchServiceCreate(t *testing.T) {
testBatch := postagetesting.MustNewBatch()
testChainState := postagetesting.NewChainState()
testBatchListener := &mockBatchCreationHandler{}

t.Run("expect put create put error", func(t *testing.T) {
svc, _, _ := newTestStoreAndService(
svc, _, _ := newTestStoreAndServiceWithListener(
testBatch.Owner,
testBatchListener,
mock.WithChainState(testChainState),
mock.WithPutErr(errTest, 0),
)
Expand All @@ -55,10 +66,15 @@ func TestBatchServiceCreate(t *testing.T) {
); err == nil {
t.Fatalf("expected error")
}
if testBatchListener.count != 0 {
t.Fatalf("unexpected batch listener count, exp %d found %d", 0, testBatchListener.count)
}
})

t.Run("passes", func(t *testing.T) {
svc, batchStore, _ := newTestStoreAndService(
svc, batchStore, _ := newTestStoreAndServiceWithListener(
testBatch.Owner,
testBatchListener,
mock.WithChainState(testChainState),
)

Expand All @@ -72,6 +88,9 @@ func TestBatchServiceCreate(t *testing.T) {
); err != nil {
t.Fatalf("got error %v", err)
}
if testBatchListener.count != 1 {
t.Fatalf("unexpected batch listener count, exp %d found %d", 1, testBatchListener.count)
}

got, err := batchStore.Get(testBatch.ID)
if err != nil {
Expand Down Expand Up @@ -263,7 +282,7 @@ func TestTransactionOk(t *testing.T) {
t.Fatal(err)
}

svc2 := batchservice.New(s, store, testLog, newMockListener())
svc2 := batchservice.New(s, store, testLog, newMockListener(), nil, nil)
if _, err := svc2.Start(10); err != nil {
t.Fatal(err)
}
Expand All @@ -283,7 +302,7 @@ func TestTransactionFail(t *testing.T) {
t.Fatal(err)
}

svc2 := batchservice.New(s, store, testLog, newMockListener())
svc2 := batchservice.New(s, store, testLog, newMockListener(), nil, nil)
if _, err := svc2.Start(10); err != nil {
t.Fatal(err)
}
Expand All @@ -292,13 +311,23 @@ func TestTransactionFail(t *testing.T) {
t.Fatalf("expect %d reset calls got %d", 1, c)
}
}
func newTestStoreAndService(opts ...mock.Option) (postage.EventUpdater, *mock.BatchStore, storage.StateStorer) {
func newTestStoreAndServiceWithListener(
owner []byte,
batchListener postage.BatchCreationListener,
opts ...mock.Option,
) (postage.EventUpdater, *mock.BatchStore, storage.StateStorer) {
s := mocks.NewStateStore()
store := mock.New(opts...)
svc := batchservice.New(s, store, testLog, newMockListener())
svc := batchservice.New(s, store, testLog, newMockListener(), owner, batchListener)
return svc, store, s
}

func newTestStoreAndService(opts ...mock.Option) (postage.EventUpdater, *mock.BatchStore, storage.StateStorer) {
testBatchListener := &mockBatchCreationHandler{}
testBatchOwner := []byte{}
return newTestStoreAndServiceWithListener(testBatchOwner, testBatchListener, opts...)
}

func putBatch(t *testing.T, store postage.Storer, b *postage.Batch) {
t.Helper()

Expand Down
2 changes: 2 additions & 0 deletions pkg/postage/mock/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func (m *mockPostage) IssuerUsable(_ *postage.StampIssuer) bool {
return true
}

func (m *mockPostage) Handle(_ *postage.Batch) {}

func (m *mockPostage) Close() error {
return nil
}
12 changes: 12 additions & 0 deletions pkg/postage/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func TestGetStampIssuer(t *testing.T) {
ps.Add(postage.NewStampIssuer(string(id), "", id, 16, 8, validBlockNumber+uint64(i)))
}
}
b := postagetesting.MustNewBatch()
b.Start = validBlockNumber
ps.Handle(b)
t.Run("found", func(t *testing.T) {
for _, id := range ids[1:4] {
st, err := ps.GetStampIssuer(id)
Expand All @@ -106,4 +109,13 @@ func TestGetStampIssuer(t *testing.T) {
}
}
})
t.Run("recovered", func(t *testing.T) {
st, err := ps.GetStampIssuer(b.ID)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if st.Label() != "recovered" {
t.Fatal("wrong issuer returned")
}
})
}

0 comments on commit b37978c

Please sign in to comment.