Skip to content

Commit

Permalink
Only stop, do not start, the jukebox
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Winter committed Nov 19, 2019
1 parent ed34650 commit add2f88
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"describe",
"context",
"before",
"beforeAll",
"beforeEach",
"after",
"afterEach",
Expand Down
13 changes: 6 additions & 7 deletions backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ const wss = io(server, { pingTimeout: 30000 })
MongodbService()

MopidyService(wss, mopidy => {
if (mopidy.playback) {
Scheduler.scheduleAutoPlayback({
stop: () => mopidy.playback.stop()
})
}

wss.on('connection', socket => {
ErrorsHandler(socket)

if (mopidy.playback) {
Scheduler.scheduleAutoPlayback({
play: () => mopidy.playback.play(),
stop: () => mopidy.playback.stop()
})
}

socket.on('message', data => {
const payload = Payload.decode(data)

Expand Down
7 changes: 1 addition & 6 deletions backend/src/lib/scheduler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ import logger from '../../config/winston'
const cron = require('node-cron')

const Scheduler = {
scheduleAutoPlayback: ({ play, stop }) => {
const playJukebox = cron.schedule('0 8 * * *', () => {
play()
logger.info('[Scheduled] Jukebox Played')
})
scheduleAutoPlayback: ({ stop }) => {
const stopJukebox = cron.schedule('0 19 * * *', () => {
stop()
logger.info('[Scheduled] Jukebox Stopped')
})

playJukebox.start()
stopJukebox.start()
}
}
Expand Down
41 changes: 17 additions & 24 deletions backend/src/lib/scheduler/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,40 @@ import logger from '../../config/winston'
const cron = require('node-cron')

describe('Scheduler', () => {
const mockPlay = jest.fn()
const mockStop = jest.fn()
const startMock = jest.fn()
const infoMock = jest.fn()

describe('scheduleAutoPlayback', () => {
beforeAll(() => {
jest.spyOn(cron, 'schedule').mockImplementation(() => {
return {
start: startMock
}
});
jest.spyOn(logger, 'info').mockImplementation(infoMock)
});

afterEach(() => {
jest.clearAllMocks()
beforeAll(() => {
jest.spyOn(cron, 'schedule').mockImplementation(() => {
return {
start: startMock
}
})
jest.spyOn(logger, 'info').mockImplementation(infoMock)
})

afterEach(() => {
jest.clearAllMocks()
})

describe('scheduleAutoPlayback', () => {
it('it should schedule two jobs, for 8am and 7pm', () => {
Scheduler.scheduleAutoPlayback({
play: mockPlay,
stop: mockStop
})
expect(cron.schedule).toHaveBeenCalledTimes(2)
expect(startMock).toHaveBeenCalledTimes(2)
expect(cron.schedule.mock.calls[0][0]).toEqual('0 8 * * *')
expect(cron.schedule.mock.calls[1][0]).toEqual('0 19 * * *')
expect(cron.schedule).toHaveBeenCalledTimes(1)
expect(startMock).toHaveBeenCalledTimes(1)
expect(cron.schedule.mock.calls[0][0]).toEqual('0 19 * * *')
})

it('when the jobs are invoked they should play and stop the jukebox and call the logger', () => {
it('when the job is invoked it should stop the jukebox and call the logger', () => {
Scheduler.scheduleAutoPlayback({
play: mockPlay,
stop: mockStop
})
cron.schedule.mock.calls[0][1]()
expect(mockPlay).toHaveBeenCalledTimes(1);
expect(infoMock).toHaveBeenCalledWith("[Scheduled] Jukebox Played")
cron.schedule.mock.calls[1][1]()
expect(mockStop).toHaveBeenCalledTimes(1)
expect(infoMock).toHaveBeenCalledWith("[Scheduled] Jukebox Stopped")
expect(infoMock).toHaveBeenCalledWith('[Scheduled] Jukebox Stopped')
})
})
})

0 comments on commit add2f88

Please sign in to comment.