Skip to content

Commit

Permalink
Unexpose example test for internal debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat committed Nov 8, 2018
1 parent 8c8f069 commit cd1cb10
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
20 changes: 10 additions & 10 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func connectionHandler(c *DeviceClient) {
}

case <-c.stableTimerCh:
c.debugPrint("Stable timer reached\n")
c.dbg.print("Stable timer reached\n")
c.stateUpdateCh <- stable

case state = <-c.stateUpdateCh:
c.debugPrintf("State updated to %s\n", state.String())
c.dbg.printf("State updated to %s\n", state.String())

switch state {
case inactive:
Expand All @@ -82,7 +82,7 @@ func connectionHandler(c *DeviceClient) {
if c.reconnectPeriod > c.opt.MaximumReconnectTime {
c.reconnectPeriod = c.opt.MaximumReconnectTime
}
c.debugPrintf("Trying to reconnect (%d ms)\n", c.reconnectPeriod/time.Millisecond)
c.dbg.printf("Trying to reconnect (%d ms)\n", c.reconnectPeriod/time.Millisecond)
if c.opt.OnConnectionLost != nil {
c.opt.OnConnectionLost(c.opt)
}
Expand All @@ -97,7 +97,7 @@ func connectionHandler(c *DeviceClient) {
psq.resubscribe()

case established:
c.debugPrint("Processing queued operations\n")
c.dbg.print("Processing queued operations\n")
go func() {
time.Sleep(c.opt.MinimumConnectionTime)
c.stableTimerCh <- true
Expand All @@ -106,12 +106,12 @@ func connectionHandler(c *DeviceClient) {

case stable:
if statePrev == established {
c.debugPrint("Connection is stable\n")
c.dbg.print("Connection is stable\n")
c.reconnectPeriod = c.opt.BaseReconnectTime
}

case terminating:
c.debugPrint("Terminating connection\n")
c.dbg.print("Terminating connection\n")
c.cli.Disconnect(250)
return

Expand All @@ -127,7 +127,7 @@ func (s *pubSubQueues) publishOrEnqueue(d *pubqueue.Data) {
go func() {
token.Wait()
if token.Error() != nil {
s.cli.debugPrintf("Failed to publish (%s)\n", token.Error())
s.cli.dbg.printf("Failed to publish (%s)\n", token.Error())
// MQTT doesn't guarantee receive order; just append to the last
s.cli.publishCh <- d
}
Expand All @@ -138,7 +138,7 @@ func (s *pubSubQueues) subscribeOrEnqueue(d *subqueue.Subscription) {
go func() {
token.Wait()
if token.Error() != nil {
s.cli.debugPrintf("Failed to subscribe (%s)\n", token.Error())
s.cli.dbg.printf("Failed to subscribe (%s)\n", token.Error())
s.cli.subscribeCh <- d
} else {
s.activeSubs[d.Topic] = d
Expand All @@ -150,7 +150,7 @@ func (s *pubSubQueues) unsubscribeOrEnqueue(d *subqueue.Subscription) {
go func() {
token.Wait()
if token.Error() != nil {
s.cli.debugPrintf("Failed to unsubscribe (%s)\n", token.Error())
s.cli.dbg.printf("Failed to unsubscribe (%s)\n", token.Error())
s.cli.subscribeCh <- d
} else {
delete(s.activeSubs, d.Topic)
Expand Down Expand Up @@ -185,7 +185,7 @@ func (s *pubSubQueues) resubscribe() {
go func(d *subqueue.Subscription) {
token.Wait()
if token.Error() != nil {
s.cli.debugPrintf("Failed to subscribe (%s)\n", token.Error())
s.cli.dbg.printf("Failed to subscribe (%s)\n", token.Error())
s.cli.subscribeCh <- d
} else {
s.activeSubs[d.Topic] = d
Expand Down
16 changes: 10 additions & 6 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ var (
DebugPrintlnBackend func(...interface{}) = log.Println
)

func (s *DeviceClient) debugPrint(a ...interface{}) {
if s.opt.Debug {
type debugOut struct {
enable bool
}

func (s *debugOut) print(a ...interface{}) {
if s.enable {
DebugPrintBackend(a...)
}
}
func (s *DeviceClient) debugPrintf(format string, a ...interface{}) {
if s.opt.Debug {
func (s *debugOut) printf(format string, a ...interface{}) {
if s.enable {
DebugPrintfBackend(format, a...)
}
}
func (s *DeviceClient) debugPrintln(a ...interface{}) {
if s.opt.Debug {
func (s *debugOut) println(a ...interface{}) {
if s.enable {
DebugPrintlnBackend(a...)
}
}
30 changes: 15 additions & 15 deletions debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ import (
"log"
)

func Example_debugPrint() {
func Example_debugOut_print() {
DebugPrintBackend = func(a ...interface{}) {
fmt.Print(a...)
}

s := &DeviceClient{opt: &Options{Debug: true}}
s.debugPrint("Test1:", true)
s = &DeviceClient{opt: &Options{Debug: false}}
s.debugPrint("Test2:", false)
s := &debugOut{true}
s.print("Test1:", true)
s = &debugOut{false}
s.print("Test2:", false)

DebugPrintBackend = log.Print

// Output:
// Test1:true
}

func Example_debugPrintf() {
func Example_debugOut_printf() {
DebugPrintfBackend = func(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

s := &DeviceClient{opt: &Options{Debug: true}}
s.debugPrintf("Formatted value 1 (%d)", 10)
s = &DeviceClient{opt: &Options{Debug: false}}
s.debugPrintf("Formatted value 2 (%d)", 11)
s := &debugOut{true}
s.printf("Formatted value 1 (%d)", 10)
s = &debugOut{false}
s.printf("Formatted value 2 (%d)", 11)

DebugPrintfBackend = log.Printf

// Output:
// Formatted value 1 (10)
}

func Example_debugPrintln() {
func Example_debugOut_println() {
DebugPrintlnBackend = func(a ...interface{}) {
fmt.Println(a...)
}

s := &DeviceClient{opt: &Options{Debug: true}}
s.debugPrintln(errors.New("Error string 1"))
s = &DeviceClient{opt: &Options{Debug: false}}
s.debugPrintln(errors.New("Error string 2"))
s := &debugOut{true}
s.println(errors.New("Error string 1"))
s = &debugOut{false}
s.println(errors.New("Error string 2"))

DebugPrintlnBackend = log.Println

Expand Down
10 changes: 6 additions & 4 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ type DeviceClient struct {
stableTimerCh chan bool
publishCh chan *pubqueue.Data
subscribeCh chan *subqueue.Subscription
dbg *debugOut
}

func New(opt *Options) *DeviceClient {
if opt.Protocol != "" || opt.Host != "" {
(&DeviceClient{opt: opt}).debugPrint("Options.Protocol and Options.Host are deprecated. Use Options.URL instead.")
(&debugOut{opt.Debug}).print("Options.Protocol and Options.Host are deprecated. Use Options.URL instead.")
opt.Url = opt.Protocol + "://" + opt.Host
}
p, err := awsiotprotocol.ByUrl(opt.Url)
Expand Down Expand Up @@ -79,14 +80,15 @@ func New(opt *Options) *DeviceClient {
stableTimerCh: make(chan bool),
publishCh: make(chan *pubqueue.Data, publishChCap),
subscribeCh: make(chan *subqueue.Subscription, subscribeChCap),
dbg: &debugOut{opt.Debug},
}

connectionLost := func(client mqtt.Client, err error) {
d.debugPrintf("Connection lost (%s)\n", err.Error())
d.dbg.printf("Connection lost (%s)\n", err.Error())
d.stateUpdateCh <- inactive
}
onConnect := func(client mqtt.Client) {
d.debugPrintf("Connection established\n")
d.dbg.printf("Connection established\n")
d.stateUpdateCh <- established
}

Expand Down Expand Up @@ -119,7 +121,7 @@ func (s *DeviceClient) connect() {
go func() {
token.Wait()
if token.Error() != nil {
s.debugPrintf("Failed to connect (%s)\n", token.Error())
s.dbg.printf("Failed to connect (%s)\n", token.Error())
s.stateUpdateCh <- inactive
}
}()
Expand Down

0 comments on commit cd1cb10

Please sign in to comment.