Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Aug 5, 2017
1 parent 447ca6c commit 68926ce
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
59 changes: 58 additions & 1 deletion Tests/MQTTnet.Core.Tests/MqttServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,64 @@ public async Task MqttServer_WillMessage()
Assert.AreEqual(1, receivedMessagesCount);
}

private MqttClient ConnectTestClient(string clientId, MqttApplicationMessage willMessage, MqttServer server)
[TestMethod]
public async Task MqttServer_Unsubscribe()
{
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
s.Start();

var c1 = ConnectTestClient("c1", null, s);
var c2 = ConnectTestClient("c2", null, s);

var receivedMessagesCount = 0;
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;

var message = new MqttApplicationMessage("a", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, false);

await c2.PublishAsync(message);
Assert.AreEqual(0, receivedMessagesCount);

await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));
await c2.PublishAsync(message);

await Task.Delay(500);
Assert.AreEqual(1, receivedMessagesCount);

await c1.Unsubscribe("a");
await c2.PublishAsync(message);

await Task.Delay(500);
Assert.AreEqual(1, receivedMessagesCount);

s.Stop();
await Task.Delay(500);

Assert.AreEqual(1, receivedMessagesCount);
}

[TestMethod]
public async Task MqttServer_Publish()
{
var s = new MqttServer(new MqttServerOptions(), new List<IMqttServerAdapter> { new TestMqttServerAdapter() });
s.Start();

var c1 = ConnectTestClient("c1", null, s);

var receivedMessagesCount = 0;
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;

var message = new MqttApplicationMessage("a", new byte[0], MqttQualityOfServiceLevel.AtLeastOnce, false);
await c1.SubscribeAsync(new TopicFilter("a", MqttQualityOfServiceLevel.AtLeastOnce));

s.Publish(message);
await Task.Delay(500);

s.Stop();

Assert.AreEqual(1, receivedMessagesCount);
}

private static MqttClient ConnectTestClient(string clientId, MqttApplicationMessage willMessage, MqttServer server)
{
var adapterA = new TestMqttCommunicationAdapter();
var adapterB = new TestMqttCommunicationAdapter();
Expand Down
8 changes: 4 additions & 4 deletions Tests/MQTTnet.Core.Tests/MqttSubscriptionsManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void MqttSubscriptionsManager_SubscribeSingleSuccess()
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
};

Assert.IsTrue(sm.IsTopicSubscribed(pp));
Assert.IsTrue(sm.IsSubscribed(pp));
}

[TestMethod]
Expand All @@ -43,7 +43,7 @@ public void MqttSubscriptionsManager_SubscribeSingleNoSuccess()
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
};

Assert.IsFalse(sm.IsTopicSubscribed(pp));
Assert.IsFalse(sm.IsSubscribed(pp));
}

[TestMethod]
Expand All @@ -62,13 +62,13 @@ public void MqttSubscriptionsManager_SubscribeAndUnsubscribeSingle()
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
};

Assert.IsTrue(sm.IsTopicSubscribed(pp));
Assert.IsTrue(sm.IsSubscribed(pp));

var up = new MqttUnsubscribePacket();
up.TopicFilters.Add("A/B/C");
sm.Unsubscribe(up);

Assert.IsFalse(sm.IsTopicSubscribed(pp));
Assert.IsFalse(sm.IsSubscribed(pp));
}
}
}

0 comments on commit 68926ce

Please sign in to comment.