-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSingleMessageTest.cs
More file actions
161 lines (125 loc) · 4.34 KB
/
Copy pathSingleMessageTest.cs
File metadata and controls
161 lines (125 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using SimpleRabbit.NetCore.Tests.implementations;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SimpleRabbit.NetCore.Tests
{
/// <summary>
/// Full integration (publsiher + subscriber) with rabbitMQ server.
/// </summary>
[TestFixture]
[NonParallelizable]
public class SingleMessageTest : IntegrationFixture
{
protected PublishService publisher;
protected QueueService queue;
private BasicMessage recieved;
protected CancellationTokenSource tokenSource;
protected Mock<ILogger<PublishService>> publisherlogger;
protected Mock<ILogger<QueueService>> subscriberlogger;
[OneTimeSetUp]
public void OneTimeSetUp()
{
publisherlogger = new Mock<ILogger<PublishService>>();
subscriberlogger = new Mock<ILogger<QueueService>>();
publisher = new PublishService(publisherlogger.Object, ExposedRabbitService.validConfig);
queue = new QueueService(ExposedRabbitService.validConfig, subscriberlogger.Object);
handler.Handler = Process;
}
private Acknowledgement Process(BasicMessage args)
{
recieved = args;
tokenSource.Cancel();
return Acknowledgement.Ack;
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
publisher.Dispose();
queue.Dispose();
handler.Handler = null;
}
[SetUp]
public void StartQueue()
{
var queueConfig = new QueueConfiguration
{
QueueName = QueueName,
PrefetchCount = 1,
OnErrorAction = QueueConfiguration.ErrorAction.RestartConnection,
ConsumerTag = "consumer"
};
tokenSource = new CancellationTokenSource();
queue.Start(queueConfig, handler);
}
[TearDown]
public void StopQueue()
{
queue.Stop();
tokenSource.Dispose();
}
[Test]
public async Task PublishEmptyMessage()
{
publisher.Publish(ExchangeName, null);
await WaitorEndEarly(tokenSource.Token);
recieved.Body.Should().BeEmpty();
}
[Test]
public async Task PublishDirect()
{
var message = "hey";
publisher.Publish("", QueueName, body: message);
await WaitorEndEarly(tokenSource.Token);
recieved.Body.Should().Be(message);
}
[Test]
public async Task PublishWithExchange()
{
var message = "hey";
publisher.Publish(ExchangeName, null, body: message);
await WaitorEndEarly(tokenSource.Token);
recieved.Body.Should().Be(message);
}
[Test]
public async Task ToExchange()
{
var message = "hey";
publisher.ToExchange(ExchangeName, body: message);
await WaitorEndEarly(tokenSource.Token);
recieved.Body.Should().Be(message);
}
[Test]
public async Task PublishWithProperties()
{
var message = "hey";
var list = new List<string> { "bob", "alice", "charles" };
var properties = publisher.GetBasicProperties();
properties.AppId = "App";
properties.Headers = new Dictionary<string, object>
{
{ "hi", list }
};
publisher.Publish(ExchangeName, null, properties, message);
await WaitorEndEarly(tokenSource.Token);
recieved.Body.Should().Be(message);
recieved.Properties.AppId.Should().Be(properties.AppId);
recieved.Headers.Should().ContainKey("hi")
.WhoseValue.Should().BeEquivalentTo(list.Select(s => Encoding.UTF8.GetBytes(s)));
}
[Test]
public async Task PublishWithException()
{
var message = "exception";
publisher.Publish(ExchangeName, null, body: message);
await WaitorEndEarly(tokenSource.Token);
recieved.Body.Should().Be(message);
}
}
}