Skip to content

Commit

Permalink
Added couple missing unit tests for protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyaxod committed Feb 26, 2024
1 parent d9aac6a commit bc5015f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
73 changes: 73 additions & 0 deletions pkg/storage/protocol/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ func TestNeedAt(t *testing.T) {

}

func TestDontNeedAt(t *testing.T) {

b := EncodeDontNeedAt(12345, 10)

off, length, err := DecodeDontNeedAt(b)
assert.NoError(t, err)
assert.Equal(t, int64(12345), off)
assert.Equal(t, int32(10), length)

// Make sure we can't decode silly things
_, _, err = DecodeDontNeedAt(nil)
assert.Error(t, err)

_, _, err = DecodeDontNeedAt([]byte{
99,
})
assert.Error(t, err)

}

func TestDirtyList(t *testing.T) {

blocks := []uint{1, 7, 100}
Expand All @@ -151,3 +171,56 @@ func TestDirtyList(t *testing.T) {
assert.Error(t, err)

}

func TestDevInfo(t *testing.T) {
b := EncodeDevInfo(&DevInfo{Size: 12345})

di, err := DecodeDevInfo(b)
assert.NoError(t, err)
assert.Equal(t, uint64(12345), di.Size)

// Make sure we can't decode silly things
_, err = DecodeDevInfo(nil)
assert.Error(t, err)

_, err = DecodeDevInfo([]byte{
99,
})
assert.Error(t, err)

}

func TestEvent(t *testing.T) {
b := EncodeEvent(&Event{Type: EventCompleted})

e, err := DecodeEvent(b)
assert.NoError(t, err)
assert.Equal(t, EventCompleted, e.Type)

// Make sure we can't decode silly things
_, err = DecodeEvent(nil)
assert.Error(t, err)

_, err = DecodeEvent([]byte{
99,
})
assert.Error(t, err)

}

func TestEventResponse(t *testing.T) {
b := EncodeEventResponse()

err := DecodeEventResponse(b)
assert.NoError(t, err)

// Make sure we can't decode silly things
err = DecodeEventResponse(nil)
assert.Error(t, err)

err = DecodeEventResponse([]byte{
99,
})
assert.Error(t, err)

}
2 changes: 2 additions & 0 deletions testing/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ func TestProtocolEvents(t *testing.T) {
// Send some events and make sure they happen at the other end...

sourceToProtocol.SendEvent(protocol.EventPreLock)
// There should be the event waiting for us already.
assert.Equal(t, 1, len(events))
e := <-events
assert.Equal(t, protocol.EventPreLock, e)
sourceToProtocol.SendEvent(protocol.EventPostLock)
Expand Down

0 comments on commit bc5015f

Please sign in to comment.