updating tests

This commit is contained in:
Prashant Gupta 2019-04-26 18:05:16 -07:00
parent 8bc345892e
commit bba12a2242
3 changed files with 94 additions and 69 deletions

View File

@ -6,9 +6,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/go-vgo/robotgo" "github.com/go-vgo/robotgo"
"github.com/prashantgupta24/activity-tracker/pkg/activity" "github.com/prashantgupta24/activity-tracker/pkg/activity"
"github.com/prashantgupta24/activity-tracker/pkg/tracker" "github.com/prashantgupta24/activity-tracker/pkg/tracker"
@ -27,7 +24,7 @@ type MouseMover struct {
const ( const (
timeout = 100 //ms timeout = 100 //ms
logDir = "log" logDir = "log"
logFileName = "logFile-amm-1" logFileName = "logFile-amm-2"
) )
//Start the main app //Start the main app
@ -47,11 +44,14 @@ func (m *MouseMover) Start() {
} }
heartbeatCh := activityTracker.Start() heartbeatCh := activityTracker.Start()
run(heartbeatCh, m) m.run(heartbeatCh, activityTracker)
} }
func run(heartbeatCh chan *tracker.Heartbeat, m *MouseMover) { func (m *MouseMover) run(heartbeatCh chan *tracker.Heartbeat, activityTracker *tracker.Instance) {
go func(m *MouseMover) { go func() {
if m.isRunning() {
return
}
logger := getLogger(m, false) //set writeToFile=true only for debugging logger := getLogger(m, false) //set writeToFile=true only for debugging
m.updateRunningStatus(true) m.updateRunningStatus(true)
movePixel := 10 movePixel := 10
@ -91,7 +91,7 @@ func run(heartbeatCh chan *tracker.Heartbeat, m *MouseMover) {
logger.Errorf("timeout happened after %vms while trying to move mouse", timeout) logger.Errorf("timeout happened after %vms while trying to move mouse", timeout)
} }
} else { } else {
logger.Infof("activity detected in the last %v seconds.", int(heartbeatInterval)) logger.Infof("activity detected in the last %v seconds.", int(activityTracker.HeartbeatInterval))
logger.Infof("Activity type:\n") logger.Infof("Activity type:\n")
for activityType, times := range heartbeat.ActivityMap { for activityType, times := range heartbeat.ActivityMap {
logger.Infof("activityType : %v times: %v\n", activityType, len(times)) logger.Infof("activityType : %v times: %v\n", activityType, len(times))
@ -110,34 +110,7 @@ func run(heartbeatCh chan *tracker.Heartbeat, m *MouseMover) {
return return
} }
} }
}(m) }()
}
func (m *MouseMover) isRunning() bool {
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.runningStatus
}
func (m *MouseMover) updateRunningStatus(isRunning bool) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.runningStatus = isRunning
}
func moveAndCheck(movePixel int, mouseMoveSuccessCh chan bool) {
currentX, currentY := robotgo.GetMousePos()
moveToX := currentX + movePixel
moveToY := currentY + movePixel
robotgo.MoveMouse(moveToX, moveToY)
//check if mouse moved. Sometimes mac users need to give
//extra permission for controlling the mouse
movedX, movedY := robotgo.GetMousePos()
if movedX == currentX && movedY == currentY {
mouseMoveSuccessCh <- false
} else {
mouseMoveSuccessCh <- true
}
} }
//Quit the app //Quit the app
@ -158,31 +131,3 @@ func GetInstance() *MouseMover {
} }
return instance return instance
} }
func getLogger(m *MouseMover, doWriteToFile bool) *log.Logger {
logger := log.New()
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
if doWriteToFile {
_, err := os.Stat(logDir)
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(logDir, os.ModePerm)
if err != nil {
log.Fatalf("error creating dir: %v", err)
}
}
}
logFile, err := os.OpenFile(logDir+"/"+logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
logger.SetOutput(logFile)
m.logFile = logFile
}
return logger
}

View File

@ -0,0 +1,64 @@
package mousemover
import (
"os"
"github.com/go-vgo/robotgo"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
func getLogger(m *MouseMover, doWriteToFile bool) *log.Logger {
logger := log.New()
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
if doWriteToFile {
_, err := os.Stat(logDir)
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(logDir, os.ModePerm)
if err != nil {
log.Fatalf("error creating dir: %v", err)
}
}
}
logFile, err := os.OpenFile(logDir+"/"+logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
logger.SetOutput(logFile)
m.logFile = logFile
}
return logger
}
func moveAndCheck(movePixel int, mouseMoveSuccessCh chan bool) {
currentX, currentY := robotgo.GetMousePos()
moveToX := currentX + movePixel
moveToY := currentY + movePixel
robotgo.MoveMouse(moveToX, moveToY)
//check if mouse moved. Sometimes mac users need to give
//extra permission for controlling the mouse
movedX, movedY := robotgo.GetMousePos()
if movedX == currentX && movedY == currentY {
mouseMoveSuccessCh <- false
} else {
mouseMoveSuccessCh <- true
}
}
func (m *MouseMover) isRunning() bool {
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.runningStatus
}
func (m *MouseMover) updateRunningStatus(isRunning bool) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.runningStatus = isRunning
}

View File

@ -6,15 +6,31 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
) )
type TestMover struct { type TestMover struct {
suite.Suite suite.Suite
activityTracker *tracker.Instance
heartbeatCh chan *tracker.Heartbeat
} }
func TestSuite(t *testing.T) { func TestSuite(t *testing.T) {
suite.Run(t, new(TestMover)) suite.Run(t, new(TestMover))
} }
//Run once before all tests
func (suite *TestMover) SetupSuite() {
heartbeatInterval := 60
workerInterval := 10
suite.activityTracker = &tracker.Instance{
HeartbeatInterval: heartbeatInterval,
WorkerInterval: workerInterval,
}
suite.heartbeatCh= make(chan *tracker.Heartbeat)
}
//Run once before each test //Run once before each test
func (suite *TestMover) SetupTest() { func (suite *TestMover) SetupTest() {
@ -23,22 +39,22 @@ func (suite *TestMover) SetupTest() {
func (suite *TestMover) TestSingleton() { func (suite *TestMover) TestSingleton() {
t := suite.T() t := suite.T()
mouseMover1 := GetInstance() mouseMover1 := GetInstance()
mouseMover1.Start() mouseMover1.run(suite.heartbeatCh, suite.activityTracker)
time.Sleep(time.Millisecond * 500) time.Sleep(time.Millisecond * 500)
mouseMover2 := GetInstance() mouseMover2 := GetInstance()
assert.True(t, mouseMover2.isRunning(), "instance should not have started") assert.True(t, mouseMover2.isRunning(), "instance should have started")
} }
func (suite *TestMover) TestAppStartAndStop() { func (suite *TestMover) TestAppStartAndStop() {
t := suite.T() t := suite.T()
mouseMover := GetInstance() mouseMover := GetInstance()
mouseMover.Start() mouseMover.run(suite.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.isRunning(), "app should have started") assert.True(t, mouseMover.isRunning(), "app should have started")
mouseMover.Quit() mouseMover.Quit()
time.Sleep(time.Millisecond * 500) //wait for app to stop time.Sleep(time.Millisecond * 1000) //wait for app to stop
assert.False(t, mouseMover.isRunning(), "app should have stopped") assert.False(t, mouseMover.isRunning(), "app should have stopped")
} }