Skip to content

Commit

Permalink
Correct golangci-lint reported issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric BAIL committed Jan 14, 2024
1 parent 6cdebfd commit f9ae33a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
20 changes: 15 additions & 5 deletions cmd/rpi-fan-sensor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ func Test_End2End(t *testing.T) {

var eg errgroup.Group
eg.Go(func() error {
return serve(mqttServer, "1", fan, temp, func() { <-stop }, func() { <-stop })
return serve(mqttServer, "1", fan, temp, func() { <-stop }, func() error {
select {
case <-stop:
return nil
case <-ctx.Done():
return ctx.Err()
}
})
})

eg.Go(func() error {
Expand Down Expand Up @@ -112,6 +119,9 @@ func Test_End2End(t *testing.T) {
test.VerifyImage(t, filepath.Join("testdata", "failed", "screenshot-"+name+".png"))
})
}

cancel()
assert.NoError(t, eg.Wait())
}

func setupMqttServer(ctx context.Context) (string, func(), error) {
Expand Down Expand Up @@ -139,7 +149,7 @@ func setupMqttServer(ctx context.Context) (string, func(), error) {
cancel := true
defer func() {
if cancel {
mqttContainer.Terminate(ctx)
_ = mqttContainer.Terminate(ctx)
}
}()

Expand All @@ -159,9 +169,9 @@ func setupMqttServer(ctx context.Context) (string, func(), error) {
if err == nil {
defer stream.Close()
b, _ := io.ReadAll(stream)
os.WriteFile("mosquitto.log", b, 0o600)
_ = os.WriteFile("mosquitto.log", b, 0o600)
}
mqttContainer.Terminate(ctx)
_ = mqttContainer.Terminate(ctx)
}, nil
}

Expand All @@ -187,6 +197,6 @@ func newPage(t *testing.T) (playwright.Page, func()) {
return page, func() {
page.Close()
browser.Close()
pw.Stop()
_ = pw.Stop()
}
}
14 changes: 11 additions & 3 deletions cmd/rpi-fan-sensor/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"context"
"errors"
"log"
"os"
"time"
Expand Down Expand Up @@ -34,15 +36,16 @@ func main() {

if err := serve("tcp://localhost:1883", id, f, t,
func() { _, _ = daemon.SdNotify(false, daemon.SdNotifyReady) },
func() {
func() error {
time.Sleep(interval / 3)
_, _ = daemon.SdNotify(false, daemon.SdNotifyWatchdog)
return nil
}); err != nil {
log.Fatal("Failure to serve:", err)
}
}

func serve(server string, id string, f fans.Fan, t cpu.Temp, ready func(), tick func()) error {
func serve(server string, id string, f fans.Fan, t cpu.Temp, ready func(), tick func() error) error {
opts := mqtt.NewClientOptions()
opts.AddBroker(server)
opts.SetClientID("sensor" + os.Args[0])
Expand All @@ -64,6 +67,11 @@ func serve(server string, id string, f fans.Fan, t cpu.Temp, ready func(), tick
log.Println(err)
}

tick()
if err := tick(); err != nil {
if errors.Is(err, context.Canceled) {
return nil
}
return err
}
}
}

0 comments on commit f9ae33a

Please sign in to comment.