Skip to content

Commit

Permalink
interface change for two Expect New functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tvanriper committed Aug 2, 2022
1 parent e70ebd5 commit 9ab13f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
15 changes: 8 additions & 7 deletions expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "time"
// Expect provides an interface for the mock io stream to use when matching incoming
// information.
type Expect interface {
Match(b []byte) (response []byte, count int, ok bool)
Duration() time.Duration
Match(b []byte) (response []byte, count int, ok bool) // Match provides a response to a given set of bytes
Duration() time.Duration // Duraction provides the amount of time to wait before responding to a read request for this response
}

// ExpectBytes provides a kind of Expect that precisely matches a sequence of bytes to
Expand Down Expand Up @@ -38,17 +38,17 @@ func (e *ExpectBytes) Match(b []byte) (response []byte, count int, ok bool) {
return response, count, ok
}

// Duration determines how long to wait to answer what is written.
// Duration responds with how long to wait before responding to a read request.
func (e *ExpectBytes) Duration() time.Duration {
return e.WaitDuration
}

// NewExpectBytes provides a convenience constructor for ExpectBytes.
func NewExpectBytes(expect []byte, respond []byte) *ExpectBytes {
func NewExpectBytes(expect []byte, respond []byte, wait time.Duration) *ExpectBytes {
return &ExpectBytes{
Expect: expect,
Respond: respond,
WaitDuration: time.Millisecond,
WaitDuration: wait,
}
}

Expand All @@ -74,15 +74,16 @@ func (e *ExpectFunc) Match(b []byte) (response []byte, count int, ok bool) {
return response, count, ok
}

// Duration provides the amount of time to wait before responding to a read request.
func (e *ExpectFunc) Duration() time.Duration {
return e.WaitDuration
}

// NewExpectFunc provides a convenience constructor for ExpectFunc.
func NewExpectFunc(fn ExpectFuncTest, response []byte) *ExpectFunc {
func NewExpectFunc(fn ExpectFuncTest, response []byte, wait time.Duration) *ExpectFunc {
return &ExpectFunc{
Test: fn,
Respond: response,
WaitDuration: time.Millisecond,
WaitDuration: wait,
}
}
13 changes: 6 additions & 7 deletions mockio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func MatchBytes(l []byte, r []byte) bool {
func TestMockIO(t *testing.T) {
m := NewMockIO()
exp := []byte{3, 4}
m.Expect(NewExpectBytes([]byte{1, 2}, exp))
m.Expect(NewExpectBytes([]byte{1, 2}, exp, 0))
m.Write([]byte{1, 2})
b := make([]byte, 2)
n, err := m.Read(b)
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestMockIOFunc(t *testing.T) {
return 2, true
}
exp := []byte{3, 4}
m.Expect(NewExpectFunc(test, exp))
m.Expect(NewExpectFunc(test, exp, 0))
m.Write([]byte{0, 1})
b := make([]byte, 2)
n, err := m.Read(b)
Expand All @@ -94,7 +94,7 @@ func TestMockIOFunc(t *testing.T) {
func TestMockIOFail(t *testing.T) {
m := NewMockIO()
exp := []byte{3, 4}
m.Expect(NewExpectBytes([]byte{1, 2}, exp))
m.Expect(NewExpectBytes([]byte{1, 2}, exp, 0))
m.Write([]byte{0, 2})
b := make([]byte, 2)
n, err := m.Read(b)
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestMockIOFail(t *testing.T) {
func TestMockClear(t *testing.T) {
m := NewMockIO()
exp := []byte{3, 4}
m.Expect(NewExpectBytes([]byte{1, 2}, exp))
m.Expect(NewExpectBytes([]byte{1, 2}, exp, 0))
l := len(m.Expects)
if l != 1 {
t.Errorf("expected 1 item, but found %d", l)
Expand All @@ -144,7 +144,7 @@ func TestMockClear(t *testing.T) {
t.Errorf("expected 0 items, but found %d", l)
}

m.Expect(NewExpectBytes([]byte{1, 2}, exp))
m.Expect(NewExpectBytes([]byte{1, 2}, exp, 0))
m.Write([]byte{3, 4})
l = len(m.holding)
if l != 2 {
Expand All @@ -170,8 +170,7 @@ func TestMockDurations(t *testing.T) {
return 2, true
}
exp := []byte{3, 4}
e := NewExpectFunc(test, exp)
e.WaitDuration = time.Duration(rand.Intn(200)) * time.Millisecond
e := NewExpectFunc(test, exp, time.Duration(rand.Intn(200))*time.Millisecond)
m.Expect(e)
start := time.Now()
m.Write([]byte{0, 1})
Expand Down

0 comments on commit 9ab13f5

Please sign in to comment.