-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTimeoutClientTest.cs
111 lines (90 loc) · 2.76 KB
/
TimeoutClientTest.cs
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
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using FaunaDB.Client;
using FaunaDB.Types;
using NUnit.Framework;
using static FaunaDB.Query.Language;
namespace Test
{
#if NETFRAMEWORK || NETCOREAPP2_0 || NETCOREAPP3_0
[TestFixture]
public class TimeoutClientTest
{
private DummyServer dummy;
private FaunaClient client;
[SetUp]
public void SetUp()
{
dummy = new DummyServer();
dummy.Start();
client = new FaunaClient("secret", "http://127.0.0.1:9999", timeout: TimeSpan.FromSeconds(10));
}
[TearDown]
public void TearDown()
{
dummy.Stop();
}
[Test]
public void TestServerTimingOut()
{
Stopwatch watch = new Stopwatch();
watch.Start();
Task<Value> task0 = client.Query(Now());
Assert.ThrowsAsync<TimeoutException>(async () => await task0);
watch.Stop();
Assert.IsTrue(task0.IsCompleted, "the task0 is completed");
Assert.IsTrue(task0.IsFaulted, "the task0 failed");
StringAssert.Contains("The operation has timed out.", task0.Exception.Message);
Assert.IsTrue(watch.Elapsed.TotalSeconds >= 10 && watch.Elapsed.TotalSeconds <= 12);
}
[Test]
public async Task TestSimpleServerResponse()
{
dummy.AcceptResponses();
Task<Value> task1 = client.Query(Add(41, 1));
await task1;
Assert.IsTrue(task1.IsCompleted, "the task1 is completed");
Assert.IsFalse(task1.IsFaulted, "the task1 is Ok");
Assert.AreEqual(42, task1.Result.To<long>().Value);
}
}
internal class DummyServer
{
private TcpListener tcpListener;
public void Start(int port = 9999)
{
IPAddress address = IPAddress.Parse("127.0.0.1");
tcpListener = new TcpListener(address, port);
tcpListener.Start();
}
public void AcceptResponses()
{
string response = @"HTTP/1.1 200 OK
X-Txn-Time: 1588276396485000
X-Read-Ops: 0
X-Write-Ops: 0
X-Query-Bytes-In: 15
X-Query-Bytes-Out: 19
X-FaunaDB-Build: dummy-server
connection: keep-alive
content-length: 19
content-type: application/json;charset=utf-8
{ ""resource"": 42 }
";
Task cliTask = new Task(() =>
{
Socket cli = tcpListener.AcceptSocket();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(response);
cli.Send(msg);
cli.Close();
});
cliTask.Start();
}
public void Stop() =>
tcpListener.Stop();
}
#endif
}