Skip to content

Commit

Permalink
pr: asserts, naming
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza El-Saawy <[email protected]>
  • Loading branch information
helsaawy committed Jul 28, 2022
1 parent 70bc382 commit aef0a8f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions hvsock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,9 @@ func TestHvSockCloseReadWriteListener(t *testing.T) {
//
// test CloseWrite()
//
_, err = c.Write([]byte(testStr))
n, err := c.Write([]byte(testStr))
u.Must(err, "server tx")
u.Assert(n == len(testStr), fmt.Sprintf("server wrote %d bytes, wanted %d", n, len(testStr)))

u.Must(hv.CloseWrite(), "server close write")

Expand All @@ -310,8 +311,9 @@ func TestHvSockCloseReadWriteListener(t *testing.T) {
// test CloseRead()
//
b := make([]byte, 256)
n, err := c.Read(b)
n, err = c.Read(b)
u.Must(err, "server read")
u.Assert(n == len(testStr), fmt.Sprintf("server read %d bytes, wanted %d", n, len(testStr)))
u.Assert(string(b[:n]) == testStr, fmt.Sprintf("server got %q; wanted %q", b[:n], testStr))

u.Must(hv.CloseRead(), "server close read")
Expand All @@ -338,14 +340,16 @@ func TestHvSockCloseReadWriteListener(t *testing.T) {
b := make([]byte, 256)
n, err := cl.Read(b)
u.Must(err, "client read")
u.Assert(n == len(testStr), fmt.Sprintf("client read %d bytes, wanted %d", n, len(testStr)))
u.Assert(string(b[:n]) == testStr, fmt.Sprintf("client got %q; wanted %q", b[:n], testStr))

n, err = cl.Read(b)
u.Assert(n == 0, "client did not get EOF")
u.Is(err, io.EOF, "client did not get EOF")

_, err = cl.Write([]byte(testStr))
n, err = cl.Write([]byte(testStr))
u.Must(err, "client write")
u.Assert(n == len(testStr), fmt.Sprintf("client wrote %d bytes, wanted %d", n, len(testStr)))

u.Wait(ch, time.Second)
u.Check()
Expand Down Expand Up @@ -592,23 +596,23 @@ func (u testUtil) Assert(b bool, msgs ...string) {
return
}
u.T.Helper()
u.T.Fatalf(_msgJoin(msgs, "failed assertion"))
u.T.Fatalf(msgJoin(msgs, "failed assertion"))
}

func (u testUtil) Is(err, target error, msgs ...string) {
if errors.Is(err, target) {
return
}
u.T.Helper()
u.T.Fatalf(_msgJoin(msgs, "got error %q; wanted %q"), err, target)
u.T.Fatalf(msgJoin(msgs, "got error %q; wanted %q"), err, target)
}

func (u testUtil) Must(err error, msgs ...string) {
if err == nil {
return
}
u.T.Helper()
u.T.Fatalf(_msgJoin(msgs, "%v"), err)
u.T.Fatalf(msgJoin(msgs, "%v"), err)
}

func (u testUtil) Wait(ch <-chan struct{}, d time.Duration, msgs ...string) {
Expand All @@ -618,10 +622,10 @@ func (u testUtil) Wait(ch <-chan struct{}, d time.Duration, msgs ...string) {
case <-ch:
case <-t.C:
u.T.Helper()
u.T.Fatalf(_msgJoin(msgs, "timed out after %v"), d)
u.T.Fatalf(msgJoin(msgs, "timed out after %v"), d)
}
}

func _msgJoin(pre []string, s string) string {
func msgJoin(pre []string, s string) string {
return strings.Join(append(pre, s), ": ")
}

0 comments on commit aef0a8f

Please sign in to comment.