Skip to content

Commit

Permalink
add unit tests for ping code
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcia Piccione authored and Marcia Piccione committed Feb 21, 2023
1 parent 1b9a8f3 commit be1893c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions clientConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func NewClient(config ClientConfig) (Client, error) {

newClient.wg.Add(2)
go newClient.checkPing(pingTimer, pinged)

go newClient.read()

return newClient, nil
Expand Down
11 changes: 11 additions & 0 deletions clientPing.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kratos

import (
"fmt"
"time"

"go.uber.org/zap"
Expand All @@ -15,40 +16,50 @@ type HandlePingMiss func() error
// given interval. If a ping is missed, we call the client's HandlePingMiss
// function.
func (c *client) checkPing(inTimer *time.Timer, pinged <-chan string) {
fmt.Println("in checkPing")
defer c.wg.Done()
// pingMiss indicates that a ping has been missed.
pingMiss := false
c.logger.Info("Watching socket for pings")
count := 0
fmt.Println("about to go into for")
// as long as we're getting pings, we continue to loop.
for !pingMiss {
fmt.Println("in select")
select {

// if we get a done signal, we leave the function.
case <-c.done:
c.logger.Info("Stopped waiting for pings")
fmt.Println("done")
return
// if we get a ping, make sure to reset the timer until the next ping.
case <-pinged:
count = 0
fmt.Println("pinged")
if !inTimer.Stop() {
<-inTimer.C
}
c.logger.Debug("Received a ping. Resetting ping timer")

inTimer.Reset(c.pingConfig.PingWait)

// if we hit the timer, we've missed a ping.
case <-inTimer.C:
fmt.Println("inTimer")
c.logger.Error("Ping miss, calling handler", zap.Int("count", count))
err := c.handlePingMiss()
if err != nil {
c.logger.Error("Error handling ping miss:", zap.Error(err))
}
if count >= c.pingConfig.MaxPingMiss {
fmt.Println("exiting ping loop")
c.logger.Error("Ping miss, exiting ping loop")
pingMiss = true
}
c.logger.Debug("Resetting ping timer")
inTimer.Reset(c.pingConfig.PingWait)
}
fmt.Println("end of checkPing")
}
}
75 changes: 75 additions & 0 deletions clientPing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package kratos

import (
"fmt"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/xmidt-org/sallust"
)

var c = ClientConfig{
DeviceName: "mac:ffffff112233",
HandlePingMiss: func() error {
fmt.Println("hi")
pingMissCount++
return nil
},
ClientLogger: nil,
PingConfig: PingConfig{PingWait: 1000000000, MaxPingMiss: 0},
}

var pingMissCount = 0

func TestPingDone(t *testing.T) {
pingMissCount = 0
newClient := &client{
deviceID: clientConfig.DeviceName,
handlePingMiss: clientConfig.HandlePingMiss,
done: make(chan struct{}, 1),
logger: sallust.Default(),
pingConfig: clientConfig.PingConfig,
wg: sync.WaitGroup{},
}

newClient.wg.Add(1)
pinged := make(chan string)
pingTimer := time.NewTimer(newClient.pingConfig.PingWait)

close(newClient.done)
newClient.checkPing(pingTimer, pinged)

newClient.wg.Wait()

assert.Equal(t, pingMissCount, 0)
}

func TestPing(t *testing.T) {
pingMissCount = 0
newClient := &client{
deviceID: c.DeviceName,
handlePingMiss: c.HandlePingMiss,
done: make(chan struct{}, 1),
logger: sallust.Default(),
pingConfig: c.PingConfig,
wg: sync.WaitGroup{},
}

newClient.wg.Add(1)
pinged := make(chan string)
pingTimer := time.NewTimer(newClient.pingConfig.PingWait)

go func() {
pinged <- "ping"
}()

time.Sleep(50)

newClient.checkPing(pingTimer, pinged)

newClient.wg.Wait()

assert.Equal(t, pingMissCount, 1)
}
5 changes: 5 additions & 0 deletions kratos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ func TestNew(t *testing.T) {

assert.Equal("127.0.0.1", testClient.Hostname())
assert.Nil(err)

}

func Error(s string) {
panic("unimplemented")
}

func TestNewBrokenMAC(t *testing.T) {
Expand Down

0 comments on commit be1893c

Please sign in to comment.