Skip to content

Commit

Permalink
Refactoring RabbitService constructor to be more UnitTest friendly.
Browse files Browse the repository at this point in the history
  • Loading branch information
houseofcat committed Apr 24, 2024
1 parent 52786f0 commit 776214d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static async Task<RabbitService> BuildRabbitServiceAsync(
compressionProvider,
loggerFactory);

await rabbitService.Publisher.StartAutoPublishAsync();
await rabbitService.StartAsync();

return rabbitService;
}
Expand Down
47 changes: 16 additions & 31 deletions src/HouseofCat.RabbitMQ/Services/RabbitService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,47 +77,26 @@ public RabbitService(
CompressionProvider = compressionProvider;
}

public RabbitService(
string fileNamePath,
ISerializationProvider serializationProvider,
IEncryptionProvider encryptionProvider = null,
ICompressionProvider compressionProvider = null,
ILoggerFactory loggerFactory = null,
Func<IPublishReceipt, ValueTask> processReceiptAsync = null)
: this(
JsonFileReader
.ReadFileAsync<RabbitOptions>(fileNamePath)
.GetAwaiter()
.GetResult(),
serializationProvider,
encryptionProvider,
compressionProvider,
loggerFactory,
processReceiptAsync)
{ }

public RabbitService(
RabbitOptions options,
ISerializationProvider serializationProvider,
IEncryptionProvider encryptionProvider = null,
ICompressionProvider compressionProvider = null,
ILoggerFactory loggerFactory = null,
Func<IPublishReceipt, ValueTask> processReceiptAsync = null) : this(
ILoggerFactory loggerFactory = null)
: this(
new ChannelPool(options),
serializationProvider,
encryptionProvider,
compressionProvider,
loggerFactory,
processReceiptAsync)
loggerFactory)
{ }

public RabbitService(
IChannelPool chanPool,
ISerializationProvider serializationProvider,
IEncryptionProvider encryptionProvider = null,
ICompressionProvider compressionProvider = null,
ILoggerFactory loggerFactory = null,
Func<IPublishReceipt, ValueTask> processReceiptAsync = null)
ILoggerFactory loggerFactory = null)
{
Guard.AgainstNull(chanPool, nameof(chanPool));
Guard.AgainstNull(serializationProvider, nameof(serializationProvider));
Expand All @@ -136,14 +115,20 @@ public RabbitService(
};

Topologer = new Topologer(ChannelPool);
}

BuildConsumers();

Publisher.StartAutoPublish(processReceiptAsync);
public async Task StartAsync(Func<IPublishReceipt, ValueTask> processReceiptAsync = null)
{
if (await _serviceLock.WaitAsync(0).ConfigureAwait(false)) return;

BuildConsumerTopologyAsync()
.GetAwaiter()
.GetResult();
try
{
BuildConsumers();
await BuildConsumerTopologyAsync();
Publisher.StartAutoPublish(processReceiptAsync);
}
finally
{ _serviceLock.Release(); }
}

public async ValueTask ShutdownAsync(bool immediately)
Expand Down

0 comments on commit 776214d

Please sign in to comment.