Skip to content

Commit

Permalink
mock: Improvements
Browse files Browse the repository at this point in the history
Use testing.TB.Cleanup to register Stop inside Start, and also use
slightly better context cancellation check.
  • Loading branch information
fishy committed Apr 24, 2022
1 parent cb66108 commit 700a5b0
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 25 deletions.
3 changes: 1 addition & 2 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ func TestEcho(t *testing.T) {

const timeout = time.Millisecond * 200

service, device := mock.StartService(t)
defer service.Stop()
_, device := mock.StartService(t)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
Expand Down
1 change: 0 additions & 1 deletion label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func TestGetLabel(t *testing.T) {
expected.Set("foo")

service, device := mock.StartService(t)
defer service.Stop()
service.RawStateLabelPayload = &lifxlan.RawStateLabelPayload{
Label: expected,
}
Expand Down
1 change: 0 additions & 1 deletion light/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func TestSetColor(t *testing.T) {
label.Set("foo")

service, device := mock.StartService(t)
defer service.Stop()
service.RawStatePayload = &light.RawStatePayload{
Label: label,
}
Expand Down
1 change: 0 additions & 1 deletion light/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func TestWrap(t *testing.T) {
label.Set("foo")

service, device := mock.StartService(t)
defer service.Stop()

t.Run(
"Normal",
Expand Down
3 changes: 0 additions & 3 deletions mock/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func Example_testGetLabel() {
expected.Set("foo")

service, device := mock.StartService(t)
defer service.Stop()
// This is the payload to be returned by the mock service.
service.RawStateLabelPayload = &lifxlan.RawStateLabelPayload{
Label: expected,
Expand Down Expand Up @@ -64,7 +63,6 @@ func Example_testGetLabelWithHandlerFunc() {
expected.Set("foo")

service, device := mock.StartService(t)
defer service.Stop()

// This defines the handler for GetLabel messages.
service.Handlers[lifxlan.GetLabel] = func(
Expand Down Expand Up @@ -112,7 +110,6 @@ func Example_testNotEnoughAcks() {
const timeout = time.Millisecond * 200

service, device := mock.StartService(t)
defer service.Stop()

rawTile1 := tile.RawTileDevice{
Width: 8,
Expand Down
23 changes: 11 additions & 12 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ func StartService(tb testing.TB) (*Service, lifxlan.Device) {
}

// Start starts the service and returns the device.
//
// It also register Stop to TB's Cleanup.
func (s *Service) Start() lifxlan.Device {
s.TB.Helper()

Expand All @@ -225,6 +227,7 @@ func (s *Service) Start() lifxlan.Device {
s.wg.Add(1)
go s.handler(conn)

s.TB.Cleanup(s.Stop)
return lifxlan.NewDevice(
conn.LocalAddr().String(),
lifxlan.ServiceUDP,
Expand All @@ -250,9 +253,9 @@ func (s *Service) Reply(
message lifxlan.MessageType,
payload []byte,
) {
select {
default:
case <-s.Context.Done():
s.TB.Helper()

if s.Context.Err() != nil {
return
}

Expand All @@ -266,24 +269,22 @@ func (s *Service) Reply(
payload,
)
if err != nil {
s.TB.Log(err)
s.TB.Logf("lifxlan/mock.Reply: Failed to generate replay message: %v", err)
return
}

select {
default:
case <-s.Context.Done():
if s.Context.Err() != nil {
return
}

n, err := conn.WriteTo(msg, addr)
if err != nil {
s.TB.Log(err)
s.TB.Logf("lifxlan/mock.Reply: Failed to write replay message: %v", err)
return
}
if n < len(msg) {
s.TB.Logf(
"lifxlan/mock.Reply: only wrote %d out of %d bytes",
"lifxlan/mock.Reply: Only wrote %d out of %d bytes",
n,
len(msg),
)
Expand All @@ -296,9 +297,7 @@ func (s *Service) handler(conn net.PacketConn) {

buf := make([]byte, lifxlan.ResponseReadBufferSize)
for {
select {
default:
case <-s.Context.Done():
if s.Context.Err() != nil {
s.TB.Log(s.Context.Err())
return
}
Expand Down
2 changes: 0 additions & 2 deletions power_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func TestGetPower(t *testing.T) {
expected := lifxlan.PowerOn

service, device := mock.StartService(t)
defer service.Stop()
service.RawStatePowerPayload = &lifxlan.RawStatePowerPayload{
Level: expected,
}
Expand Down Expand Up @@ -121,7 +120,6 @@ func TestSetPower(t *testing.T) {
expected := lifxlan.PowerOff

service, device := mock.StartService(t)
defer service.Stop()

var called bool

Expand Down
1 change: 0 additions & 1 deletion tile/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ func TestColorsAPIs(t *testing.T) {
label.Set("foo")

service, device := mock.StartService(t)
defer service.Stop()
service.RawStatePayload = &light.RawStatePayload{
Label: label,
}
Expand Down
1 change: 0 additions & 1 deletion tile/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func TestWrap(t *testing.T) {
label.Set("foo")

service, device := mock.StartService(t)
defer service.Stop()
service.RawStatePayload = &light.RawStatePayload{
Label: label,
}
Expand Down
1 change: 0 additions & 1 deletion version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ func TestGetHardwareVersion(t *testing.T) {
}

service, device := mock.StartService(t)
defer service.Stop()
service.RawStateVersionPayload = &lifxlan.RawStateVersionPayload{
Version: expected,
}
Expand Down

0 comments on commit 700a5b0

Please sign in to comment.