Simply-imitated-SQS-for-testing
Simply imitated SQS for testing
Simple SQS class that can be used for local test.
You do not even need to use Docker.
install
% npm install @abetomo/simply-imitated-sqs
Application example
Replace sqs
instance
'use strict'
const AWS = require('aws-sdk')
const sqs = (() => {
if (process.env.LOCAL_TEST === '1') {
const SimplyImitatedSQS = require('@abetomo/simply-imitated-sqs')
return new SimplyImitatedSQS()
}
return new AWS.SQS({
region: 'us-east-1',
apiVersion: '2012-11-05'
})
})()
const queueUrl = 'https://sqs.us-east-1.amazonaws.com/xxx/test'
Promise.resolve().then(() => {
const params = {
QueueUrl: queueUrl,
MessageBody: 'hoge' + (new Date()).toString()
}
return new Promise(resolve => {
sqs.sendMessage(params, (err, data) => {
if (err) console.error(err)
console.log('+++\n%s\n+++', JSON.stringify(data, null, ' '))
resolve()
})
})
}).then(() => {
const params = { QueueUrl: queueUrl }
return new Promise(resolve => {
sqs.receiveMessage(params, (err, data) => {
if (err) console.error(err)
console.log('===\n%s\n===', JSON.stringify(data, null, ' '))
resolve(data)
})
})
}).then(data => {
const params = {
QueueUrl: queueUrl,
ReceiptHandle: data.Messages[0].ReceiptHandle
}
return new Promise(resolve => {
sqs.deleteMessage(params, (err, data) => {
if (err) console.error(err)
console.log('---\n%s\n---', JSON.stringify(data, null, ' '))
resolve()
})
})
}).then(() => {
if (process.env.LOCAL_TEST === '1') {
sqs.clear()
}
})
Starting http server
'use strict'
const AWS = require('aws-sdk')
const sqs = new AWS.SQS({
region: 'us-east-1',
apiVersion: '2012-11-05'
})
const server = new (require('@abetomo/simply-imitated-sqs').Server)()
var queueUrl = 'https://sqs.us-east-1.amazonaws.com/xxx/test'
if (process.env.LOCAL_TEST === '1') {
queueUrl = server.run({
port: '1234',
host: 'localhost'
})
}
Promise.resolve().then(() => {
const params = {
QueueUrl: queueUrl,
MessageBody: 'hoge' + (new Date()).toString()
}
return new Promise(resolve => {
sqs.sendMessage(params, (err, data) => {
if (err) console.error(err)
console.log('+++\n%s\n+++', JSON.stringify(data, null, ' '))
resolve()
})
})
}).then(() => {
const params = { QueueUrl: queueUrl }
return new Promise(resolve => {
sqs.receiveMessage(params, (err, data) => {
if (err) console.error(err)
console.log('===\n%s\n===', JSON.stringify(data, null, ' '))
resolve(data)
})
})
}).then(data => {
const params = {
QueueUrl: queueUrl,
ReceiptHandle: data.Messages[0].ReceiptHandle
}
return new Promise(resolve => {
sqs.deleteMessage(params, (err, data) => {
if (err) console.error(err)
console.log('---\n%s\n---', JSON.stringify(data, null, ' '))
resolve()
})
})
}).then(() => {
if (process.env.LOCAL_TEST === '1') {
console.log('server shutdown')
server.close()
}
})
Execution
# Access AWS.
% node example.js
# Local Completion Test
% LOCAL_TEST=1 node example.js