Skip to content

Commit

Permalink
feat: upgrade to middy v2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
theburningmonk committed Apr 25, 2021
1 parent 98ee3b4 commit ad0410f
Show file tree
Hide file tree
Showing 29 changed files with 178 additions and 493 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const captureCorrelationIds = require('../index')

global.console.log = jest.fn()

const invokeDynamoHandler = (event, awsRequestId, sampleDebugLogRate, handlerF, recordF, done) => {
const handler = middy((event, context, cb) => {
const invokeDynamoHandler = async (event, awsRequestId, sampleDebugLogRate, handlerF, recordF) => {
const handler = middy(async (event, context) => {
// check the correlation IDs outside the context of a record are correct
handlerF(CorrelationIds.get())

Expand All @@ -18,18 +18,10 @@ const invokeDynamoHandler = (event, awsRequestId, sampleDebugLogRate, handlerF,

// check the correlation IDs outside the context of a record are correct
handlerF(CorrelationIds.get())

cb(null)
})
handler.use(captureCorrelationIds({ sampleDebugLogRate }))

handler(event, { awsRequestId }, (err, result) => {
if (err) {
throw err
}

if (done) done()
})
await handler(event, { awsRequestId })
}

const dynamo = require('./event-templates/dynamo-new-old.json')
Expand Down Expand Up @@ -59,9 +51,9 @@ const genDynamoEventWithoutNewImage = () => {

const dynamoTests = () => {
describe('when sampleDebugLogRate = 0', () => {
it('always sets debug-log-enabled to false', () => {
it('always sets debug-log-enabled to false', async () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEvent(), requestId, 0,
await invokeDynamoHandler(genDynamoEvent(), requestId, 0,
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand All @@ -75,9 +67,9 @@ const dynamoTests = () => {
})

describe('when event lacks NewImage', () => {
it('should set default correlation id', () => {
it('should set default correlation id', async () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEventWithoutNewImage(), requestId, 0,
await invokeDynamoHandler(genDynamoEventWithoutNewImage(), requestId, 0,
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand All @@ -92,9 +84,9 @@ const dynamoTests = () => {
})

describe('when sampleDebugLogRate = 1', () => {
it('always sets debug-log-enabled to true', () => {
it('always sets debug-log-enabled to true', async () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEvent(), requestId, 1,
await invokeDynamoHandler(genDynamoEvent(), requestId, 1,
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('true')
Expand All @@ -108,9 +100,9 @@ const dynamoTests = () => {
})

describe('when correlation ID is not provided in the event', () => {
it('sets it to the AWS Request ID', () => {
it('sets it to the AWS Request ID', async () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEvent(), requestId, 0,
await invokeDynamoHandler(genDynamoEvent(), requestId, 0,
x => {
// correlation IDs at the handler level
expect(x['x-correlation-id']).toBe(requestId)
Expand All @@ -126,9 +118,9 @@ const dynamoTests = () => {
})

describe('when call-chain-length is not provided in the event', () => {
it('sets it to 1', () => {
it('sets it to 1', async () => {
const requestId = uuid()
invokeDynamoHandler(genDynamoEvent(), requestId, 0,
await invokeDynamoHandler(genDynamoEvent(), requestId, 0,
x => {}, // n/a
record => {
const x = record.correlationIds.get()
Expand All @@ -144,7 +136,7 @@ const dynamoTests = () => {
let userId
let requestId

beforeEach((done) => {
beforeEach(async () => {
id = uuid()
userId = uuid()

Expand All @@ -157,11 +149,11 @@ const dynamoTests = () => {

const event = genDynamoEvent(correlationIds)
requestId = uuid()
invokeDynamoHandler(event, requestId, 0, x => {
await invokeDynamoHandler(event, requestId, 0, x => {
handlerCorrelationIds = x
}, aRecord => {
record = aRecord
}, done)
})
})

it('still has the correct handler correlation IDs', () => {
Expand Down Expand Up @@ -195,7 +187,7 @@ const dynamoTests = () => {
let record
let id

beforeEach((done) => {
beforeEach(async () => {
id = uuid()

const correlationIds = {
Expand All @@ -204,10 +196,9 @@ const dynamoTests = () => {
}

const event = genDynamoEvent(correlationIds)
invokeDynamoHandler(event, uuid(), 0,
await invokeDynamoHandler(event, uuid(), 0,
() => {},
aRecord => { record = aRecord },
done)
aRecord => { record = aRecord })
})

it('increments it by 1', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const captureCorrelationIds = require('../index')

global.console.log = jest.fn()

const invokeFirehoseHandler = (event, awsRequestId, sampleDebugLogRate, handlerF, recordF, done) => {
const handler = middy((event, context, cb) => {
const invokeFirehoseHandler = async (event, awsRequestId, sampleDebugLogRate, handlerF, recordF) => {
const handler = middy(async (event, context) => {
// check the correlation IDs outside the context of a record are correct
handlerF(CorrelationIds.get())

Expand All @@ -17,18 +17,10 @@ const invokeFirehoseHandler = (event, awsRequestId, sampleDebugLogRate, handlerF

// check the correlation IDs outside the context of a record are correct
handlerF(CorrelationIds.get())

cb(null)
})
handler.use(captureCorrelationIds({ sampleDebugLogRate }))

handler(event, { awsRequestId }, (err, result) => {
if (err) {
throw err
}

if (done) done()
})
await handler(event, { awsRequestId })
}

const firehose = require('./event-templates/firehose.json')
Expand All @@ -52,9 +44,9 @@ const genFirehoseEventWithoutJSON = (correlationIDs = {}) => {

const firehoseTests = () => {
describe('when sampleDebugLogRate = 0', () => {
it('always sets debug-log-enabled to false', () => {
it('always sets debug-log-enabled to false', async () => {
const requestId = uuid()
invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
await invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand All @@ -68,9 +60,9 @@ const firehoseTests = () => {
})

describe('when event lacks JSON payload', () => {
it('should ignore the event', () => {
it('should ignore the event', async () => {
const requestId = uuid()
invokeFirehoseHandler(genFirehoseEventWithoutJSON(), requestId, 0,
await invokeFirehoseHandler(genFirehoseEventWithoutJSON(), requestId, 0,
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('false')
Expand All @@ -83,9 +75,9 @@ const firehoseTests = () => {
})

describe('when sampleDebugLogRate = 1', () => {
it('always sets debug-log-enabled to true', () => {
it('always sets debug-log-enabled to true', async () => {
const requestId = uuid()
invokeFirehoseHandler(genFirehoseEvent(), requestId, 1,
await invokeFirehoseHandler(genFirehoseEvent(), requestId, 1,
x => {
expect(x['awsRequestId']).toBe(requestId)
expect(x['debug-log-enabled']).toBe('true')
Expand All @@ -99,9 +91,9 @@ const firehoseTests = () => {
})

describe('when correlation ID is not provided in the event', () => {
it('sets it to the AWS Request ID', () => {
it('sets it to the AWS Request ID', async () => {
const requestId = uuid()
invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
await invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
x => {
// correlation IDs at the handler level
expect(x['x-correlation-id']).toBe(requestId)
Expand All @@ -117,9 +109,9 @@ const firehoseTests = () => {
})

describe('when call-chain-length is not provided in the event', () => {
it('sets it to 1', () => {
it('sets it to 1', async () => {
const requestId = uuid()
invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
await invokeFirehoseHandler(genFirehoseEvent(), requestId, 0,
x => {}, // n/a
record => {
const x = record.correlationIds.get()
Expand All @@ -135,7 +127,7 @@ const firehoseTests = () => {
let userId
let requestId

beforeEach((done) => {
beforeEach(async () => {
id = uuid()
userId = uuid()

Expand All @@ -148,11 +140,11 @@ const firehoseTests = () => {

const event = genFirehoseEvent(correlationIds)
requestId = uuid()
invokeFirehoseHandler(event, requestId, 0, x => {
await invokeFirehoseHandler(event, requestId, 0, x => {
handlerCorrelationIds = x
}, aRecord => {
record = aRecord
}, done)
})
})

it('still has the correct handler correlation IDs', () => {
Expand Down Expand Up @@ -186,7 +178,7 @@ const firehoseTests = () => {
let record
let id

beforeEach((done) => {
beforeEach(async () => {
id = uuid()

const correlationIds = {
Expand All @@ -195,10 +187,9 @@ const firehoseTests = () => {
}

const event = genFirehoseEvent(correlationIds)
invokeFirehoseHandler(event, uuid(), 0,
await invokeFirehoseHandler(event, uuid(), 0,
() => {},
aRecord => { record = aRecord },
done)
aRecord => { record = aRecord })
})

it('increments it by 1', () => {
Expand Down

0 comments on commit ad0410f

Please sign in to comment.