Skip to content

Commit

Permalink
test: add TestPacket{Read,Write}Context
Browse files Browse the repository at this point in the history
Add some basic tests to make sure that context timeout/cancel will
unblock packet i/o and make packet{Read,Write} return the context's err.
  • Loading branch information
enr0n committed Aug 8, 2023
1 parent 94e401b commit fb4503d
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions vici/client_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"bytes"
"context"
"encoding/binary"
"errors"
"net"
"reflect"
"testing"
"time"
)

func TestPacketWrite(t *testing.T) {
Expand Down Expand Up @@ -119,3 +121,55 @@ func TestPacketRead(t *testing.T) {

<-done
}

func TestPacketWriteContext(t *testing.T) {
client, srvr := net.Pipe()
defer client.Close()
defer srvr.Close()

cc := &clientConn{
conn: client,
}

ctx, cancel := context.WithCancel(context.Background())
cancel()

err := cc.packetWrite(ctx, goldNamedPacket)
if !errors.Is(err, context.Canceled) {
t.Fatalf("Expected cancel on packet write, but got %v", err)
}

ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

err = cc.packetWrite(ctx, goldNamedPacket)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Expected timeout on packet write, but got %v", err)
}
}

func TestPacketReadContext(t *testing.T) {
client, srvr := net.Pipe()
defer client.Close()
defer srvr.Close()

cc := &clientConn{
conn: client,
}

ctx, cancel := context.WithCancel(context.Background())
cancel()

_, err := cc.packetRead(ctx)
if !errors.Is(err, context.Canceled) {
t.Fatalf("Expected cancel on packet read, but got %v", err)
}

ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

_, err = cc.packetRead(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Expected timeout on packet read, but got %v", err)
}
}

0 comments on commit fb4503d

Please sign in to comment.