From 19e0318359398bd755355450340459d5ca42c232 Mon Sep 17 00:00:00 2001 From: Prashant Gupta Date: Thu, 2 May 2019 17:58:01 -0700 Subject: [PATCH] adding tests --- pkg/mousemover/mouseMover.go | 4 +--- pkg/mousemover/mouseMoverUtil.go | 4 ++-- pkg/mousemover/mouseMover_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pkg/mousemover/mouseMover.go b/pkg/mousemover/mouseMover.go index db6461e..42c6a2d 100644 --- a/pkg/mousemover/mouseMover.go +++ b/pkg/mousemover/mouseMover.go @@ -46,10 +46,8 @@ func (m *MouseMover) run(heartbeatCh chan *tracker.Heartbeat, activityTracker *t } 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 - // var lastMoved time.Time - // didNotMoveTimes := 0 for { select { case heartbeat := <-heartbeatCh: diff --git a/pkg/mousemover/mouseMoverUtil.go b/pkg/mousemover/mouseMoverUtil.go index b686a69..3034aa1 100644 --- a/pkg/mousemover/mouseMoverUtil.go +++ b/pkg/mousemover/mouseMoverUtil.go @@ -9,7 +9,7 @@ import ( 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.Formatter = &logrus.TextFormatter{ 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 { log.Fatalf("error opening file: %v", err) } diff --git a/pkg/mousemover/mouseMover_test.go b/pkg/mousemover/mouseMover_test.go index 1e4112f..29e41e2 100644 --- a/pkg/mousemover/mouseMover_test.go +++ b/pkg/mousemover/mouseMover_test.go @@ -1,6 +1,7 @@ package mousemover import ( + "os" "testing" "time" @@ -57,11 +58,29 @@ func (suite *TestMover) TestSingleton() { 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() { t := suite.T() mouseMover := GetInstance() + state := &state{ + override: &override{ + valueToReturn: true, + }, + } + mouseMover.state = state heartbeatCh := make(chan *tracker.Heartbeat) + mouseMover.run(heartbeatCh, suite.activityTracker) time.Sleep(time.Millisecond * 500) //wait for app to start 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 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 machineWakeActivityMap := make(map[activity.Type][]time.Time) var wakeTimeArray []time.Time