Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken committed Apr 3, 2024
1 parent 6426286 commit 0045c2d
Show file tree
Hide file tree
Showing 7 changed files with 563 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ DomainEventTracker.Peek().Should().BeEmpty();


### MinimalDomainEvents.Dispatcher
Holds the ScopedDomainEventDispatcher class which creates a scope on construction, used for scoping the raising of domain events to the lifetime of this class. Resolves all registered IDispatchDomainEvents interface implementations for dispatching the raised domain events. Dispatches all domain events raised in its own scope or deeper scopes.
Holds the ScopedDomainEventDispatcher class which creates a scope on construction, used for scoping the raising of domain events to the lifetime of this class. Resolves all registered IDispatchDomainEvents interface implementations for dispatching the raised domain events. Dispatches all domain events raised in its own scope.

### MinimalDomainEvents.Dispatcher.MediatR
Contains the MediatorDispatcher, which dispatches the domain events using MediatR. The behavior uses the lifetime of the ScopedDomainEventDispatcher to capture raised domain events during its lifetime and dispatches them when the RequestHandlerDelegate completes successfully. Make sure your domain events implement both IDomainEvent and INotification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ internal sealed class MongoDbOutboxRecordPersister : IPersistOutboxRecords
private readonly IOutboxRecordCollectionProvider _outboxRecordCollectionProvider;
private readonly IMongoSessionProvider _mongoSessionProvider;

public MongoDbOutboxRecordPersister(IOutboxRecordCollectionProvider outboxRecordCollectionProvider, IMongoSessionProvider transactionProvider)
public MongoDbOutboxRecordPersister(IOutboxRecordCollectionProvider outboxRecordCollectionProvider, IMongoSessionProvider mongoSessionProvider)
{
_outboxRecordCollectionProvider = outboxRecordCollectionProvider;
_mongoSessionProvider = transactionProvider;
_mongoSessionProvider = mongoSessionProvider;
}

public async Task PersistIndividually(IReadOnlyCollection<OutboxRecord> outboxRecords, CancellationToken cancellationToken)
Expand Down
119 changes: 119 additions & 0 deletions test/MinimalDomainEvents.Outbox.MongoDb.UnitTests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM ubuntu:jammy

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN set -eux; \
groupadd --gid 999 --system mongodb; \
useradd --uid 999 --system --gid mongodb --home-dir /data/db mongodb; \
mkdir -p /data/db /data/configdb; \
chown -R mongodb:mongodb /data/db /data/configdb

RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
ca-certificates \
jq \
numactl \
procps \
; \
rm -rf /var/lib/apt/lists/*

# grab gosu for easy step-down from root (https://github.com/tianon/gosu/releases)
ENV GOSU_VERSION 1.17
# grab "js-yaml" for parsing mongod's YAML config files (https://github.com/nodeca/js-yaml/releases)
ENV JSYAML_VERSION 3.13.1

RUN set -eux; \
\
savedAptMark="$(apt-mark showmanual)"; \
apt-get update; \
apt-get install -y --no-install-recommends \
gnupg \
wget \
; \
rm -rf /var/lib/apt/lists/*; \
\
# download/install gosu
dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \
export GNUPGHOME="$(mktemp -d)"; \
gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
gpgconf --kill all; \
rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \
\
# download/install js-yaml
mkdir -p /opt/js-yaml/; \
wget -O /opt/js-yaml/js-yaml.js "https://github.com/nodeca/js-yaml/raw/${JSYAML_VERSION}/dist/js-yaml.js"; \
wget -O /opt/js-yaml/package.json "https://github.com/nodeca/js-yaml/raw/${JSYAML_VERSION}/package.json"; \
ln -s /opt/js-yaml/js-yaml.js /js-yaml.js; \
# TODO some sort of download verification here
\
# download/install MongoDB PGP keys
export GNUPGHOME="$(mktemp -d)"; \
wget -O KEYS 'https://pgp.mongodb.com/server-7.0.asc'; \
gpg --batch --import KEYS; \
mkdir -p /etc/apt/keyrings; \
gpg --batch --export --armor 'E58830201F7DD82CD808AA84160D26BB1785BA38' > /etc/apt/keyrings/mongodb.asc; \
gpgconf --kill all; \
rm -rf "$GNUPGHOME" KEYS; \
\
apt-mark auto '.*' > /dev/null; \
apt-mark manual $savedAptMark > /dev/null; \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
\
# smoke test
chmod +x /usr/local/bin/gosu; \
gosu --version; \
gosu nobody true

RUN mkdir /docker-entrypoint-initdb.d

# Allow build-time overrides (eg. to build image with MongoDB Enterprise version)
# Options for MONGO_PACKAGE: mongodb-org OR mongodb-enterprise
# Options for MONGO_REPO: repo.mongodb.org OR repo.mongodb.com
# Example: docker build --build-arg MONGO_PACKAGE=mongodb-enterprise --build-arg MONGO_REPO=repo.mongodb.com .
ARG MONGO_PACKAGE=mongodb-org
ARG MONGO_REPO=repo.mongodb.org
ENV MONGO_PACKAGE=${MONGO_PACKAGE} MONGO_REPO=${MONGO_REPO}

ENV MONGO_MAJOR 7.0
RUN echo "deb [ signed-by=/etc/apt/keyrings/mongodb.asc ] http://$MONGO_REPO/apt/ubuntu jammy/${MONGO_PACKAGE%-unstable}/$MONGO_MAJOR multiverse" | tee "/etc/apt/sources.list.d/${MONGO_PACKAGE%-unstable}.list"

# https://docs.mongodb.org/master/release-notes/7.0/
ENV MONGO_VERSION 7.0.7
# 03/13/2024, https://github.com/mongodb/mongo/tree/cfb08e1ab7ef741b4abdd0638351b322514c45bd

RUN set -x \
# installing "mongodb-enterprise" pulls in "tzdata" which prompts for input
&& export DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
&& apt-get install -y \
${MONGO_PACKAGE}=$MONGO_VERSION \
${MONGO_PACKAGE}-server=$MONGO_VERSION \
${MONGO_PACKAGE}-shell=$MONGO_VERSION \
${MONGO_PACKAGE}-mongos=$MONGO_VERSION \
${MONGO_PACKAGE}-tools=$MONGO_VERSION \
${MONGO_PACKAGE}-database=$MONGO_VERSION \
${MONGO_PACKAGE}-database-tools-extra=$MONGO_VERSION \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /var/lib/mongodb \
&& mv /etc/mongod.conf /etc/mongod.conf.orig

VOLUME /data/db /data/configdb

# ensure that if running as custom user that "mongosh" has a valid "HOME"
# https://github.com/docker-library/mongo/issues/524
ENV HOME /data/db

COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh --replSet rs0"]

EXPOSE 27017
CMD ["mongod"]
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@
<ProjectReference Include="..\..\src\MinimalDomainEvents.Outbox\MinimalDomainEvents.Outbox.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="docker-entrypoint.sh">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Dockerfile">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Testcontainers.MongoDb;
using DotNet.Testcontainers.Builders;
using Testcontainers.MongoDb;

namespace MinimalDomainEvents.Outbox.MongoDb.UnitTests;
[CollectionDefinition("MongoDb Integration")]
Expand All @@ -13,10 +14,11 @@ public sealed class MongoContainerFixture : IAsyncLifetime
public MongoContainerFixture()
{
_container = new MongoDbBuilder()
.WithImage("mongo:7.0.8-rc0-jammy")
.WithCleanUp(false)
.WithAutoRemove(false)
.WithEntrypoint("docker-entrypoint.sh mongod --replSet rs0")
.WithImage("mongo:latest")
.WithUsername("")
.WithPassword("")
.WithCommand("--replSet", "rs0")
.WithWaitStrategy(Wait.ForUnixContainer())
.Build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MessagePack;
using Microsoft.Extensions.DependencyInjection;
using MinimalDomainEvents.Contract;
using MinimalDomainEvents.Core;
using MinimalDomainEvents.Dispatcher;
using MinimalDomainEvents.Dispatcher.Abstractions;
using MinimalDomainEvents.Outbox.Abstractions;
Expand All @@ -25,7 +26,7 @@ public async Task DisposeAsync()
}

[Fact]
internal async Task Using_Transations_Persists_OutboxRecords_Within_Same_Transaction()
internal async Task Using_Transactions_Persists_OutboxRecords_Within_Same_Transaction()
{
var mongoClient = CreateClient();
IServiceCollection serviceCollection = new ServiceCollection();
Expand All @@ -39,12 +40,10 @@ internal async Task Using_Transations_Persists_OutboxRecords_Within_Same_Transac
dispatcherBuilder.Services.AddSingleton(Mock.Of<IDispatchDomainEvents>());
});
var serviceProvider = serviceCollection.BuildServiceProvider();
var dispatcher = serviceProvider.GetRequiredService<IDispatchDomainEvents>();
var dispatcher = serviceProvider.GetRequiredService<IScopedDomainEventDispatcher>();

await dispatcher.Dispatch(new[]
{
new TestEvent("SomeValue")
});
dispatcher.RaiseDomainEvent(new TestEvent("SomeValue"));
await dispatcher.DispatchAndClear();

var transactionProvider = serviceProvider.GetRequiredService<ITransactionProvider>();
var retriever = serviceProvider.GetRequiredService<IRetrieveOutboxRecords>();
Expand Down
Loading

0 comments on commit 0045c2d

Please sign in to comment.