test: 完善消息单元测试

oneRain 2020-04-26 15:50:37 +08:00
parent b70e6c7bbb
commit f852fe2264
1 changed files with 85 additions and 5 deletions

View File

@ -1,14 +1,12 @@
using NUnit.Framework;
using System;
using System.Threading;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Collections.Generic;
using LeanCloud;
using LeanCloud.Common;
using LeanCloud.Storage;
using LeanCloud.Realtime;
using static System.Console;
using static NUnit.Framework.TestContext;
namespace Realtime.Test {
public class Message {
@ -36,16 +34,25 @@ namespace Realtime.Test {
}
[Test]
[Order(0)]
public async Task Send() {
AutoResetEvent are = new AutoResetEvent(false);
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
int count = 0;
m2.OnMessage = (conv, msg) => {
WriteLine(msg.Id);
if (msg is LCIMImageMessage imageMsg) {
WriteLine($"-------- url: {imageMsg.Url}");
count++;
} else if (msg is LCIMFileMessage fileMsg) {
WriteLine($"-------- name: {fileMsg.Format}");
count++;
} else if (msg is LCIMTextMessage textMsg) {
WriteLine($"-------- text: {textMsg.Text}");
count++;
}
if (count >= 3) {
tcs.SetResult(null);
}
};
@ -64,6 +71,79 @@ namespace Realtime.Test {
LCIMFileMessage fileMessage = new LCIMFileMessage(file);
await conversation.Send(fileMessage);
Assert.NotNull(fileMessage.Id);
await tcs.Task;
}
[Test]
[Order(1)]
public async Task AckAndRead() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
m2.OnMessage = async (conv, msg) => {
await conv.Read();
};
m1.OnMessageDelivered = (conv, msgId) => {
WriteLine($"{msgId} is delivered.");
};
m1.OnMessageRead = (conv, msgId) => {
WriteLine($"{msgId} is read.");
tcs.SetResult(null);
};
LCIMTextMessage textMessage = new LCIMTextMessage("hello");
LCIMMessageSendOptions options = new LCIMMessageSendOptions {
Receipt = true
};
await conversation.Send(textMessage, options);
await tcs.Task;
}
[Test]
[Order(2)]
public async Task Recall() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
m2.OnMessageRecalled = (conv, msgId) => {
WriteLine($"{msgId} is recalled.");
tcs.SetResult(null);
};
LCIMTextMessage textMessage = new LCIMTextMessage("I will be recalled.");
await conversation.Send(textMessage);
await Task.Delay(1000);
await conversation.RecallMessage(textMessage);
await tcs.Task;
}
[Test]
[Order(3)]
public async Task Update() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
m2.OnMessageUpdated = (conv, msg) => {
Assert.True(msg is LCIMTextMessage);
LCIMTextMessage textMessage = msg as LCIMTextMessage;
Assert.AreEqual(textMessage.Text, "world");
WriteLine($"{msg.Id} is updated");
tcs.SetResult(null);
};
LCIMTextMessage oldMessage = new LCIMTextMessage("hello");
await conversation.Send(oldMessage);
await Task.Delay(1000);
LCIMTextMessage newMessage = new LCIMTextMessage("world");
await conversation.UpdateMessage(oldMessage, newMessage);
await tcs.Task;
}
[Test]
[Order(4)]
public async Task Query() {
ReadOnlyCollection<LCIMMessage> messages = await conversation.QueryMessages();
Assert.Greater(messages.Count, 0);
foreach (LCIMMessage message in messages) {
Assert.AreEqual(message.ConversationId, conversation.Id);
Assert.NotNull(message.Id);
WriteLine(message.Id);
}
}
}
}