adding tests

This commit is contained in:
Prashant Gupta 2019-05-02 17:58:01 -07:00
parent 118bce49e0
commit 19e0318359
3 changed files with 31 additions and 5 deletions

View File

@ -46,10 +46,8 @@ func (m *MouseMover) run(heartbeatCh chan *tracker.Heartbeat, activityTracker *t
} }
state.updateRunningStatus(true) state.updateRunningStatus(true)
logger := getLogger(m, false) //set writeToFile=true only for debugging logger := getLogger(m, false, logFileName) //set writeToFile=true only for debugging
movePixel := 10 movePixel := 10
// var lastMoved time.Time
// didNotMoveTimes := 0
for { for {
select { select {
case heartbeat := <-heartbeatCh: case heartbeat := <-heartbeatCh:

View File

@ -9,7 +9,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func getLogger(m *MouseMover, doWriteToFile bool) *log.Logger { func getLogger(m *MouseMover, doWriteToFile bool, filename string) *log.Logger {
logger := log.New() logger := log.New()
logger.Formatter = &logrus.TextFormatter{ logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true, FullTimestamp: true,
@ -26,7 +26,7 @@ func getLogger(m *MouseMover, doWriteToFile bool) *log.Logger {
} }
} }
logFile, err := os.OpenFile(logDir+"/"+logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) logFile, err := os.OpenFile(logDir+"/"+filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {
log.Fatalf("error opening file: %v", err) log.Fatalf("error opening file: %v", err)
} }

View File

@ -1,6 +1,7 @@
package mousemover package mousemover
import ( import (
"os"
"testing" "testing"
"time" "time"
@ -57,11 +58,29 @@ func (suite *TestMover) TestSingleton() {
assert.True(t, mouseMover2.state.isRunning(), "instance should have started") assert.True(t, mouseMover2.state.isRunning(), "instance should have started")
} }
func (suite *TestMover) TestLogFile() {
t := suite.T()
mouseMover := GetInstance()
logFileName := "test1"
getLogger(mouseMover, true, logFileName)
filePath := logDir + "/" + logFileName
assert.FileExists(t, filePath, "log file should exist")
os.RemoveAll(filePath)
}
func (suite *TestMover) TestSystemSleepAndWake() { func (suite *TestMover) TestSystemSleepAndWake() {
t := suite.T() t := suite.T()
mouseMover := GetInstance() mouseMover := GetInstance()
state := &state{
override: &override{
valueToReturn: true,
},
}
mouseMover.state = state
heartbeatCh := make(chan *tracker.Heartbeat) heartbeatCh := make(chan *tracker.Heartbeat)
mouseMover.run(heartbeatCh, suite.activityTracker) mouseMover.run(heartbeatCh, suite.activityTracker)
time.Sleep(time.Millisecond * 500) //wait for app to start time.Sleep(time.Millisecond * 500) //wait for app to start
assert.True(t, mouseMover.state.isRunning(), "instance should have started") assert.True(t, mouseMover.state.isRunning(), "instance should have started")
@ -79,6 +98,15 @@ func (suite *TestMover) TestSystemSleepAndWake() {
time.Sleep(time.Millisecond * 500) //wait for it to be registered time.Sleep(time.Millisecond * 500) //wait for it to be registered
assert.True(t, mouseMover.state.isSystemSleeping(), "machine should be sleeping now") assert.True(t, mouseMover.state.isSystemSleeping(), "machine should be sleeping now")
//assert app is sleeping
heartbeatCh <- &tracker.Heartbeat{
WasAnyActivity: false,
}
time.Sleep(time.Millisecond * 500) //wait for it to be registered
assert.True(t, time.Time.IsZero(state.getLastMouseMovedTime()), "should be default but is ", state.getLastMouseMovedTime())
assert.Equal(t, state.getDidNotMoveCount(), 0, "should be 0")
//fake a machine-wake activity //fake a machine-wake activity
machineWakeActivityMap := make(map[activity.Type][]time.Time) machineWakeActivityMap := make(map[activity.Type][]time.Time)
var wakeTimeArray []time.Time var wakeTimeArray []time.Time