Skip to content

Instantly share code, notes, and snippets.

@KarneAsada
Last active November 10, 2018 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KarneAsada/6935bf998e6c6a57796eef14bdac835e to your computer and use it in GitHub Desktop.
Save KarneAsada/6935bf998e6c6a57796eef14bdac835e to your computer and use it in GitHub Desktop.
const config = require('../../config')
const cloudTasks = require('@google-cloud/tasks')
const queue = {}
queue.transcodeVideo = async (data) => {
const qClient = new cloudTasks.CloudTasksClient()
const task = {
appEngineHttpRequest: {
httpMethod : 'POST',
relativeUri : '/transcode/video',
headers : { 'Content-Type': 'application/json' },
body : Buffer.from(JSON.stringify(data)).toString('base64'),
},
}
if (config.VERSION) {
task.appEngineHttpRequest.appEngineRouting = { version: config.VERSION }
}
const parent = qClient.queuePath(
config.google.projectId,
config.google.location,
config.google.transcodeQ
)
const qRequest = { parent, task }
try {
const response = await qClient.createTask(qRequest)
console.info(`Created cloud task ${response[0].name}`)
return true
} catch (err) {
console.error(`Error in createTask: ${err.message || err}`)
return false
}
}
module.exports = queue
const cloudTasks = require('@google-cloud/tasks')
const queue = require('../queue')
const config = require('../../../config')
const createTask = () => jest.fn()
const parent = 'mockParent'
const CloudTasksClient = () => ({
createTask : () => createTask,
queuePath : () => parent
})
jest.mock('@google-cloud/tasks', () => { CloudTasksClient })
// jest.spyOn(cloudTasks, 'CloudTasksClient').mockImplementation(CloudTasksClient)
describe('The transcodeVideo method', () => {
test('should send a request to Cloud Tasks to queue a video transcode', async () => {
const data = {
videoId : 123,
workspaceID : 123,
}
const task = {
appEngineHttpRequest: {
httpMethod : 'POST',
relativeUri : '/transcode/video',
headers : { 'Content-Type': 'application/json' },
body : Buffer.from(JSON.stringify(data)).toString('base64'),
},
}
const input = { parent, task }
await queue.transcodeVideo(data)
expect(createTask).toHaveBeenCalledTimes(1)
expect(createTask).toHaveBeenCalledWith(input)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment