Skip to content

Commit

Permalink
Merge pull request #25 from prashantgupta24/test-cover
Browse files Browse the repository at this point in the history
improving tests
  • Loading branch information
prashantgupta24 authored Apr 10, 2019
2 parents 9969639 + 13b1938 commit b0a1014
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 18 deletions.
15 changes: 12 additions & 3 deletions pkg/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (tracker *Instance) StartWithHandlers(handlers ...handler.Instance) (heartb
case activity := <-tracker.activityCh:
if activity.State != nil {
trackerLog.Debugf("received state change request for %v activity", activity.Type)
tracker.state = activity.State
tracker.updateSystemState(activity.State)
}
timeNow := time.Now()
activityMap[activity.Type] = append(activityMap[activity.Type], timeNow)
Expand Down Expand Up @@ -113,7 +113,16 @@ func (tracker *Instance) Start() (heartbeatCh chan *Heartbeat) {
return tracker.StartWithHandlers(getAllHandlers()...)
}

//GetTrackerSystemState returns a copy of state of the system which is being used by the tracker
func (tracker *Instance) GetTrackerSystemState() system.State {
//getTrackerSystemState returns a copy of state of the system which is being used by the tracker
func (tracker *Instance) getTrackerSystemState() system.State {
tracker.mutex.RLock()
defer tracker.mutex.RUnlock()
return *tracker.state
}

//updateSystemState updates state of the system which is being used by the tracker
func (tracker *Instance) updateSystemState(state *system.State) {
tracker.mutex.Lock()
defer tracker.mutex.Unlock()
tracker.state = state
}
78 changes: 63 additions & 15 deletions pkg/tracker/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/prashantgupta24/activity-tracker/internal/pkg/handler"
"github.com/prashantgupta24/activity-tracker/pkg/activity"
"github.com/prashantgupta24/activity-tracker/pkg/system"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -117,14 +118,6 @@ func (suite *TestTracker) TestDupHandlerRegistration() {
assert.Equal(t, 1, len(tracker.handlers), "duplicate handlers should not be registered")
}

// func (suite *TestTracker) TestActivityHandlersNumEqual() {
// t := suite.T()
// numActivities := len(suite.activities)
// numHandlers := len(getAllHandlers())

// assert.Equal(t, numHandlers, numActivities, "tracker should have equal handlers and activities")
// }

func (suite *TestTracker) TestActivitiesOneByOne() {
t := suite.T()
tracker := suite.tracker
Expand Down Expand Up @@ -223,11 +216,7 @@ func (suite *TestTracker) TestTrackerStartAndQuit() {
t := suite.T()
tracker := suite.tracker

numHandlers := len(getAllHandlers())
heartbeatCh := tracker.Start()

assert.Equal(t, numHandlers, len(tracker.handlers), "tracker should have started with %v handlers by default", numHandlers)

heartbeatCh := tracker.StartWithHandlers(handler.TestHandler())
//close
var wg sync.WaitGroup
isHeartbeatStopped := make(chan bool)
Expand Down Expand Up @@ -255,5 +244,64 @@ func (suite *TestTracker) TestTrackerStartAndQuit() {
}
}

//test whether test handler can change state
//test validateHandlers
func (suite *TestTracker) TestValidateHandlers() {
t := suite.T()
machineSleepHandler := handler.MachineSleepHandler()

//case 1
handlers := []handler.Instance{
handler.MouseClickHandler(), handler.MouseCursorHandler(),
handler.ScreenChangeHandler(),
}
validatedHandlers := validateHandlers(handlers...)
assert.Contains(t, validatedHandlers, machineSleepHandler, "validateHandler() should add machine sleep handler by default")

//case 2
handlers = []handler.Instance{
handler.MachineSleepHandler(),
}
validatedHandlers = validateHandlers(handlers...)
assert.Contains(t, validatedHandlers, machineSleepHandler, "validateHandler() should add machine sleep handler by default")
}

func (suite *TestTracker) TestValidateHandlersOnStart() {
t := suite.T()
machineSleepHandlerType := handler.MachineSleepHandler().Type()

tracker := suite.tracker

tracker.StartWithHandlers(handler.TestHandler())
assert.NotContains(t, tracker.handlers, machineSleepHandlerType, "validateHandler() should not add machine sleep handler type in test")
}

func (suite *TestTracker) TestTrackerStateChange() {
t := suite.T()
tracker := suite.tracker
tracker.StartWithHandlers()

oldState := tracker.getTrackerSystemState()
assert.False(t, oldState.IsSystemSleep, "system sleep state should be false by default")

tracker.activityCh <- &activity.Instance{
Type: activity.TestActivity,
State: &system.State{
IsSystemSleep: true,
},
}

newState := tracker.getTrackerSystemState()
assert.True(t, newState.IsSystemSleep, "system sleep state should be over-written to true")

}

func (suite *TestTracker) TestTrackerStateChangeByValue() {
t := suite.T()
tracker := suite.tracker
tracker.StartWithHandlers()

oldState := tracker.getTrackerSystemState()
oldState.IsSystemSleep = true //this should not affect the state object since it is a copy

newState := tracker.getTrackerSystemState()
assert.False(t, newState.IsSystemSleep, "system sleep state should not change")
}
2 changes: 2 additions & 0 deletions pkg/tracker/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tracker

import (
"sync"
"time"

"github.com/prashantgupta24/activity-tracker/internal/pkg/handler"
Expand All @@ -14,6 +15,7 @@ type Instance struct {
WorkerInterval int //the interval at which you want the checks to happen within a heartbeat (in seconds, default 5s)
LogLevel string
LogFormat string
mutex sync.RWMutex
state *system.State //maintains the state of the system
isTest bool //only for testing purposes
activityCh chan *activity.Instance
Expand Down

0 comments on commit b0a1014

Please sign in to comment.