github.com/xmidt-org/webpa-common@v1.11.9/device/devicehealth/health_test.go (about) 1 package devicehealth 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/mock" 8 "github.com/xmidt-org/webpa-common/device" 9 "github.com/xmidt-org/webpa-common/health" 10 ) 11 12 func testListenerOnDeviceEventConnect(t *testing.T) { 13 var ( 14 assert = assert.New(t) 15 dispatcher = new(mockDispatcher) 16 listener = &Listener{Dispatcher: dispatcher} 17 18 expectedStats = health.Stats{ 19 DeviceCount: 1, 20 TotalConnectionEvents: 1, 21 } 22 23 actualStats = health.Stats{} 24 ) 25 26 dispatcher.On("SendEvent", mock.AnythingOfType("health.HealthFunc")).Once(). 27 Run(func(arguments mock.Arguments) { 28 hf := arguments.Get(0).(health.HealthFunc) 29 hf(actualStats) 30 }) 31 32 listener.OnDeviceEvent(&device.Event{Type: device.Connect}) 33 assert.Equal(expectedStats, actualStats) 34 35 dispatcher.AssertExpectations(t) 36 } 37 38 func testListenerOnDeviceEventDisconnect(t *testing.T) { 39 var ( 40 assert = assert.New(t) 41 dispatcher = new(mockDispatcher) 42 listener = &Listener{Dispatcher: dispatcher} 43 44 expectedStats = health.Stats{ 45 DeviceCount: 0, 46 TotalConnectionEvents: 1, 47 TotalDisconnectionEvents: 1, 48 } 49 50 actualStats = health.Stats{ 51 DeviceCount: 1, 52 TotalConnectionEvents: 1, 53 } 54 ) 55 56 dispatcher.On("SendEvent", mock.AnythingOfType("health.HealthFunc")).Once(). 57 Run(func(arguments mock.Arguments) { 58 hf := arguments.Get(0).(health.HealthFunc) 59 hf(actualStats) 60 }) 61 62 listener.OnDeviceEvent(&device.Event{Type: device.Disconnect}) 63 assert.Equal(expectedStats, actualStats) 64 65 dispatcher.AssertExpectations(t) 66 } 67 68 func testListenerOnDeviceEventTransactionComplete(t *testing.T) { 69 var ( 70 assert = assert.New(t) 71 dispatcher = new(mockDispatcher) 72 listener = &Listener{Dispatcher: dispatcher} 73 74 expectedStats = health.Stats{ 75 TotalWRPRequestResponseProcessed: 1, 76 } 77 78 actualStats = health.Stats{} 79 ) 80 81 dispatcher.On("SendEvent", mock.AnythingOfType("health.HealthFunc")).Once(). 82 Run(func(arguments mock.Arguments) { 83 hf := arguments.Get(0).(health.HealthFunc) 84 hf(actualStats) 85 }) 86 87 listener.OnDeviceEvent(&device.Event{Type: device.TransactionComplete}) 88 assert.Equal(expectedStats, actualStats) 89 90 dispatcher.AssertExpectations(t) 91 } 92 93 func TestListener(t *testing.T) { 94 t.Run("OnDeviceEvent", func(t *testing.T) { 95 t.Run("Connect", testListenerOnDeviceEventConnect) 96 t.Run("Disconnect", testListenerOnDeviceEventDisconnect) 97 t.Run("TransactionComplete", testListenerOnDeviceEventTransactionComplete) 98 }) 99 }