Skip to content

Commit

Permalink
Add more basic topic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragemil committed Sep 14, 2023
1 parent aff908b commit aee7043
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 17 deletions.
89 changes: 72 additions & 17 deletions publisher/test/LeanPipe.IntegrationTests/BasicTopicTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net.Http.Json;
using FluentAssertions;
using LeanPipe.IntegrationTests.App;
using LeanPipe.TestClient;
Expand All @@ -25,37 +24,93 @@ public BasicTopicTests()
}

[Fact]
public async Task Basic_topic_subscriber_receives_both_kinds_of_messages()
public async Task Subscriber_receives_both_kinds_of_notifications_when_subscribed()
{
var topic = new UnauthorizedTopic { TopicId = Guid.NewGuid() };

await leanPipeClient.SubscribeSuccessAsync(topic);
leanPipeClient.NotificationsOn(topic).Should().BeEmpty();

var notificationTask = leanPipeClient.GetNextNotificationTaskOn(topic);
await httpClient.PostAsJsonAsync(
"/publish_unauthorized",
new NotificationDataDTO
{
await httpClient.PublishToUnauthorizedTopicAsync(new() {
TopicId = topic.TopicId,
Kind = NotificationKindDTO.Greeting,
Name = "Tester",
}
);
});

(await notificationTask).Should()
.BeEquivalentTo(new GreetingNotificationDTO { Greeting = "Hello Tester" });

notificationTask = leanPipeClient.GetNextNotificationTaskOn(topic);
await httpClient.PostAsJsonAsync(
"/publish_unauthorized",
new NotificationDataDTO
{
TopicId = topic.TopicId,
Kind = NotificationKindDTO.Farewell,
Name = "Tester",
}
);
await httpClient.PublishToUnauthorizedTopicAsync(new() {
TopicId = topic.TopicId,
Kind = NotificationKindDTO.Farewell,
Name = "Tester",
});

(await notificationTask).Should()
.BeEquivalentTo(new FarewellNotificationDTO { Farewell = "Goodbye Tester" });

await leanPipeClient.UnsubscribeSuccessAsync(topic);

await httpClient.PublishToUnauthorizedTopicAsync(new() {
TopicId = topic.TopicId,
Kind = NotificationKindDTO.Farewell,
Name = "other Tester",
});

await Task.Delay(1000);
leanPipeClient.NotificationsOn(topic).Should().HaveCount(2);
}

[Fact]
public async Task Subscriber_does_not_receive_messages_from_other_topic_instances()
{
var topic = new UnauthorizedTopic { TopicId = Guid.NewGuid() };
var otherTopic = new UnauthorizedTopic { TopicId = Guid.NewGuid() };

await leanPipeClient.SubscribeSuccessAsync(topic);

await httpClient.PublishToUnauthorizedTopicAsync(new() {
TopicId = otherTopic.TopicId,
Kind = NotificationKindDTO.Greeting,
Name = "Tester",
});

await Task.Delay(1000);
leanPipeClient.NotificationsOn(topic).Should().BeEmpty();
leanPipeClient.NotificationsOn(otherTopic).Should().BeEmpty();

await leanPipeClient.UnsubscribeSuccessAsync(topic);
}

[Fact]
public async Task Resubscribing_after_disconnect_works()
{
var topic = new UnauthorizedTopic { TopicId = Guid.NewGuid() };

await leanPipeClient.SubscribeSuccessAsync(topic);
leanPipeClient.NotificationsOn(topic).Should().BeEmpty();

var notificationTask = leanPipeClient.GetNextNotificationTaskOn(topic);
await httpClient.PublishToUnauthorizedTopicAsync(new() {
TopicId = topic.TopicId,
Kind = NotificationKindDTO.Greeting,
Name = "Tester",
});

(await notificationTask).Should()
.BeEquivalentTo(new GreetingNotificationDTO { Greeting = "Hello Tester" });

await leanPipeClient.DisconnectAsync();
await leanPipeClient.SubscribeSuccessAsync(topic);

notificationTask = leanPipeClient.GetNextNotificationTaskOn(topic);
await httpClient.PublishToUnauthorizedTopicAsync(new() {
TopicId = topic.TopicId,
Kind = NotificationKindDTO.Farewell,
Name = "Tester",
});

(await notificationTask).Should()
.BeEquivalentTo(new FarewellNotificationDTO { Farewell = "Goodbye Tester" });
Expand Down
27 changes: 27 additions & 0 deletions publisher/test/LeanPipe.IntegrationTests/PublishingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Net.Http.Json;
using LeanPipe.IntegrationTests.App;

namespace LeanPipe.IntegrationTests;

public static class PublishingExtensions
{
public static Task PublishToUnauthorizedTopicAsync(this HttpClient client, NotificationDataDTO notificationData)
{
return PostAsync(client, "/publish_unauthorized", notificationData);
}

public static Task PublishToAuthorizedTopicAsync(this HttpClient client, NotificationDataDTO notificationData)
{
return PostAsync(client, "/publish_authorized", notificationData);
}

private static async Task PostAsync<TPayload>(HttpClient client, string uri, TPayload payload)
{
using var response = await client.PostAsJsonAsync(
uri,
payload
);

response.EnsureSuccessStatusCode();
}
}

0 comments on commit aee7043

Please sign in to comment.