From 23b9880fae0ad4435de471c0acb0cf8665b11cc9 Mon Sep 17 00:00:00 2001 From: Trey Van Riper Date: Mon, 1 Aug 2022 14:41:23 -0400 Subject: [PATCH] adding ability to delay response --- expect.go | 32 ++++++++++++++++++++++++-------- mockio.go | 1 + 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/expect.go b/expect.go index 8dcc3c2..2821731 100644 --- a/expect.go +++ b/expect.go @@ -1,16 +1,20 @@ package mockio +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) + Wait() } // ExpectBytes provides a kind of Expect that precisely matches a sequence of bytes to // respond with another sequence of bytes. type ExpectBytes struct { - Expect []byte - Respond []byte + Expect []byte + Respond []byte + WaitDuration time.Duration } // Match implements the Expect interface for ExpectBytes. @@ -34,11 +38,17 @@ func (e *ExpectBytes) Match(b []byte) (response []byte, count int, ok bool) { return response, count, ok } +// Wait waits for the duration of this structure before continuing. +func (e *ExpectBytes) Wait() { + time.Sleep(e.WaitDuration) +} + // NewExpectBytes provides a convenience constructor for ExpectBytes. func NewExpectBytes(expect []byte, respond []byte) *ExpectBytes { return &ExpectBytes{ - Expect: expect, - Respond: respond, + Expect: expect, + Respond: respond, + WaitDuration: 0, } } @@ -50,8 +60,9 @@ type ExpectFuncTest func(b []byte) (count int, ok bool) // ExpectFunc provides a kind of Expect that tests a sequence of bytes with a function. type ExpectFunc struct { - Test ExpectFuncTest - Respond []byte + Test ExpectFuncTest + Respond []byte + WaitDuration time.Duration } // Match implements the Expect interface for ExpectFunc. @@ -63,10 +74,15 @@ func (e *ExpectFunc) Match(b []byte) (response []byte, count int, ok bool) { return response, count, ok } +func (e *ExpectFunc) Wait() { + time.Sleep(e.WaitDuration) +} + // NewExpectFunc provides a convenience constructor for ExpectFunc. func NewExpectFunc(fn ExpectFuncTest, response []byte) *ExpectFunc { return &ExpectFunc{ - Test: fn, - Respond: response, + Test: fn, + Respond: response, + WaitDuration: 0, } } diff --git a/mockio.go b/mockio.go index f784858..cfeb9b2 100644 --- a/mockio.go +++ b/mockio.go @@ -57,6 +57,7 @@ func (m *MockIO) Write(data []byte) (n int, err error) { if !ok { continue } + test.Wait() respond = append(respond, response...) m.holding = m.holding[count:] break