-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestWellKnown.cs
More file actions
142 lines (130 loc) · 4.55 KB
/
TestWellKnown.cs
File metadata and controls
142 lines (130 loc) · 4.55 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
using System.Net;
using System.Text;
using System.Text.Json;
using Weaviate.Client.Rest;
namespace Weaviate.Client.Tests.Unit;
public class TestWellKnown
{
private class FakeHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, HttpResponseMessage> _responder;
public List<HttpRequestMessage> Requests { get; } = new();
public FakeHandler(Func<HttpRequestMessage, HttpResponseMessage> responder)
{
_responder = responder;
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken
)
{
// Intercept requests to the Meta endpoint and return a dummy response
if (
request.RequestUri != null
&& request.RequestUri.AbsolutePath.EndsWith(WeaviateEndpoints.Meta())
&& request.Method == HttpMethod.Get
)
{
var dummyMeta = new
{
hostname = "http://localhost:8080",
version = "1.28.0",
modules = new { },
grpcMaxMessageSize = 10485760UL,
};
var json = JsonSerializer.Serialize(dummyMeta);
var content = new StringContent(json, Encoding.UTF8, "application/json");
return Task.FromResult(
new HttpResponseMessage(HttpStatusCode.OK) { Content = content }
);
}
Requests.Add(request);
return Task.FromResult(_responder(request));
}
}
[Fact]
public async Task Live_ReturnsTrue_On200()
{
var handler = new FakeHandler(req => new HttpResponseMessage(HttpStatusCode.OK));
var client = new WeaviateClient(new ClientConfiguration(), handler);
Assert.True(await client.Live());
Assert.Equal(
"v1/.well-known/live",
handler.Requests.Single().RequestUri!.AbsolutePath.TrimStart('/')
);
}
[Fact]
public async Task IsReady_ReturnsTrue_On200()
{
var handler = new FakeHandler(req =>
{
if (req.RequestUri!.AbsolutePath.EndsWith("ready"))
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
return new HttpResponseMessage(HttpStatusCode.OK);
});
var client = new WeaviateClient(new ClientConfiguration(), handler);
Assert.True(await client.IsReady());
}
[Fact]
public async Task WaitUntilReady_SucceedsBeforeTimeout()
{
int calls = 0;
var handler = new FakeHandler(req =>
{
if (req.RequestUri!.AbsolutePath.EndsWith("ready"))
{
calls++;
if (calls < 3)
{
return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
}
return new HttpResponseMessage(HttpStatusCode.OK);
}
return new HttpResponseMessage(HttpStatusCode.OK);
});
var client = new WeaviateClient(new ClientConfiguration(), handler);
var result = await client.WaitUntilReady(
TimeSpan.FromSeconds(2),
CancellationToken.None,
TimeSpan.FromMilliseconds(10)
);
Assert.True(result);
Assert.True(calls >= 3);
}
[Fact]
public async Task WaitUntilReady_TimesOut()
{
var handler = new FakeHandler(req => new HttpResponseMessage(
HttpStatusCode.ServiceUnavailable
));
var client = new WeaviateClient(new ClientConfiguration(), handler);
var result = await client.WaitUntilReady(
TimeSpan.FromMilliseconds(100),
CancellationToken.None,
TimeSpan.FromMilliseconds(10)
);
Assert.False(result);
}
[Fact]
public async Task WaitUntilReady_Cancellation()
{
var cts = new CancellationTokenSource();
int calls = 0;
var handler = new FakeHandler(req =>
{
calls++;
return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
});
var client = new WeaviateClient(new ClientConfiguration(), handler);
var task = client.WaitUntilReady(
TimeSpan.FromSeconds(5),
cts.Token,
TimeSpan.FromMilliseconds(10)
);
cts.CancelAfter(50);
await Assert.ThrowsAsync<TaskCanceledException>(async () => await task);
Assert.True(calls > 0);
}
}