Introducing EventNet: a tool for testing EventBridge events

Steve Morland
on:tech
4 min readDec 8, 2023

--

Testing asynchronous events is one of the more significant challenges of being able to validate event-driven architecture.

In the synchronous world, we have a direct response to any action, but when triggering asynchronous events, we have to be able to collect them after the fact.

There are many different ways to tackle this problem, all of which have benefits and trade-offs. We’ve never been 100% happy with all the current solutions at Leighton. Over the past year, we have developed our internal way of collecting these as part of customer projects, but we thought it was only fitting to release this publicly.

We’ve entirely rewritten an internal tool and upgraded the dependencies to track the latest CDK release and use v3 of the AWS SDK. As such, there are a few features that we still have to move over. The Leighton team will continue to add and refine.

Amazon EventBridge

For us at Leighton, EventBridge is one of the most exciting technologies for building event-driven applications with AWS. It allows us to build genuinely decoupled services that are entirely serverless.

EventBridge is a serverless service that uses events to connect application components together, making it easier for you to build scalable event-driven applications. Event-driven architecture is a style of building loosely-coupled software systems that work together by emitting and responding to events. Event-driven architecture can help you boost agility and build reliable, scalable applications.

What is EventNet?

EventNet is made up of three parts:

  1. A CDK construct to be used on test builds that captures events and sends them through a WebSocket. This real-time delivery avoids using queues or other mechanisms to get them to the test suite as soon as possible.
  2. An EventNet client connects to your WebSocket and collects events captured by the CDK construct, enabling you to test event producers. The client also lets you send events to the bus to test event consumers.
  3. A Jest Assertion to check event fidelity against JSON schemas.

The CDK Construct

Though quite a complex construct to deliver events to a Jest Suite, this gives a great experience in the test suite by having nearly instant feedback; we no longer have to poll for events on queues.

Please ensure the construct is not used in production; it is designed only for collecting and testing events.

The EventNet Client

Connecting your Jest test to the WebSocket, our JavaScript application collects the events and delivers them back to your test suite for further analysis.

You can see it in action here: https://github.com/stevemorland/eventnet-example/blob/master/_TESTS_/db-to-eb.test.ts

describe("Basic Test for Producer > ", () => {
test("Add data to DDB, capture outcome", async () => {
const eventNet = await EventNetClient.create();
await eventNet.waitForOpenSocket();
const item = {
pk: uuidv4(),
sk: "someSKValue",
someValue: "another value",
};
await saveToDynamo(dbname, item);
const events = await eventNet.matchEnvelope("*", "*", 1, 100000);
await eventNet.waitForClosedSocket();
expect(events[0]).toMatchSchema(producerSchema);
expect(events).toHaveLength(1);
console.log(events);
await eventNet.closeClient();
});
});
  1. The above example creates a client; we save some data into a DynamoDB table which is piped into EventBridge.
  2. Our EventNet client is waiting for the data collected as an array. This example catches any source, any detail type, is expecting one event and has an optional timeout.
  3. Once events are collected, we can run assertions against the object to check that it matches our expectations.

Jest Assertion

We have been using JSONSchemas heavily to design and test event-driven architectures. JSONSchema is an incredibly verbose representation of an object, something you can’t achieve with strong typing alone.

As we had been using these in our testing, wrapping all these together into one tool was a great fit.

We’d also recommend using something like EventBridge Atlas to centralise your event schemas: https://eventbridge-atlas.netlify.app/

--

--