forked from surrealdb/surrealdb.net
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
2,992 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project> | ||
<ItemGroup> | ||
<PackageReference Include="CSharpier.MsBuild" Version="0.25.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="CSharpier.MsBuild" Version="0.25.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
SurrealDb.Net.LiveQuery.Tests/Abstract/BaseLiveQueryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace SurrealDb.Net.LiveQuery.Tests.Abstract; | ||
|
||
public abstract class BaseLiveQueryTests | ||
{ | ||
protected static readonly TimeSpan Timeout = TimeSpan.FromSeconds(2); | ||
|
||
protected Task WaitLiveQueryCreationAsync(int timeMultiplier = 1) | ||
{ | ||
return Task.Delay(100 * timeMultiplier); | ||
} | ||
|
||
protected Task WaitLiveQueryNotificationAsync() | ||
{ | ||
return Task.Delay(100); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using SurrealDb.Net.Exceptions; | ||
using SurrealDb.Net.Models.Response; | ||
|
||
namespace SurrealDb.Net.LiveQuery.Tests; | ||
|
||
public class KillTests | ||
{ | ||
[Fact] | ||
public async Task ShouldNotBeSupportedOnHttpProtocol() | ||
{ | ||
const string url = "http://localhost:8000"; | ||
|
||
Func<Task> func = async () => | ||
{ | ||
await using var surrealDbClientGenerator = new SurrealDbClientGenerator(); | ||
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo(); | ||
|
||
using var client = surrealDbClientGenerator.Create(url); | ||
await client.SignIn(new RootAuth { Username = "root", Password = "root" }); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
|
||
var liveQueryUuid = Guid.NewGuid(); | ||
|
||
await client.Kill(liveQueryUuid); | ||
}; | ||
|
||
await func.Should().ThrowAsync<NotSupportedException>(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldKillActiveLiveQueryOnWsProtocol() | ||
{ | ||
const string url = "ws://localhost:8000/rpc"; | ||
|
||
Func<Task> func = async () => | ||
{ | ||
await using var surrealDbClientGenerator = new SurrealDbClientGenerator(); | ||
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo(); | ||
|
||
using var client = surrealDbClientGenerator.Create(url); | ||
await client.SignIn(new RootAuth { Username = "root", Password = "root" }); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
|
||
var response = await client.Query("LIVE SELECT * FROM test;"); | ||
|
||
if (response.FirstResult is not SurrealDbOkResult okResult) | ||
throw new Exception("Expected a SurrealDbOkResult"); | ||
|
||
var liveQueryUuid = okResult.GetValue<Guid>(); | ||
|
||
await client.Kill(liveQueryUuid); | ||
}; | ||
|
||
await func.Should().NotThrowAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldFailToKillInexistantLiveQueryOnWsProtocol() | ||
{ | ||
const string url = "ws://localhost:8000/rpc"; | ||
|
||
Func<Task> func = async () => | ||
{ | ||
await using var surrealDbClientGenerator = new SurrealDbClientGenerator(); | ||
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo(); | ||
|
||
using var client = surrealDbClientGenerator.Create(url); | ||
await client.SignIn(new RootAuth { Username = "root", Password = "root" }); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
|
||
var liveQueryUuid = Guid.NewGuid(); | ||
|
||
await client.Kill(liveQueryUuid); | ||
}; | ||
|
||
await func.Should() | ||
.ThrowAsync<SurrealDbException>() | ||
.WithMessage( | ||
"There was a problem with the database: Can not execute KILL statement using id '$id'" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using SurrealDb.Net.LiveQuery.Tests.Abstract; | ||
using SurrealDb.Net.LiveQuery.Tests.Models; | ||
using SurrealDb.Net.Models.LiveQuery; | ||
using SurrealDb.Net.Models.Response; | ||
|
||
namespace SurrealDb.Net.LiveQuery.Tests; | ||
|
||
public class ListenLiveTests : BaseLiveQueryTests | ||
{ | ||
[Fact] | ||
public async Task ShouldNotBeSupportedOnHttpProtocol() | ||
{ | ||
const string url = "http://localhost:8000"; | ||
|
||
Func<Task> func = async () => | ||
{ | ||
await using var surrealDbClientGenerator = new SurrealDbClientGenerator(); | ||
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo(); | ||
|
||
using var client = surrealDbClientGenerator.Create(url); | ||
await client.SignIn(new RootAuth { Username = "root", Password = "root" }); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
|
||
var liveQueryUuid = Guid.NewGuid(); | ||
|
||
await using var liveQuery = client.ListenLive<int>(liveQueryUuid); | ||
}; | ||
|
||
await func.Should().ThrowAsync<NotSupportedException>(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldReceiveData() | ||
{ | ||
const string url = "ws://localhost:8000/rpc"; | ||
|
||
var allResults = new List<SurrealDbLiveQueryResponse>(); | ||
|
||
Func<Task> func = async () => | ||
{ | ||
await using var surrealDbClientGenerator = new SurrealDbClientGenerator(); | ||
var dbInfo = surrealDbClientGenerator.GenerateDatabaseInfo(); | ||
|
||
using var client = surrealDbClientGenerator.Create(url); | ||
await client.SignIn(new RootAuth { Username = "root", Password = "root" }); | ||
await client.Use(dbInfo.Namespace, dbInfo.Database); | ||
|
||
var response = await client.Query("LIVE SELECT * FROM test;"); | ||
|
||
if (response.FirstResult is not SurrealDbOkResult okResult) | ||
throw new Exception("Expected a SurrealDbOkResult"); | ||
|
||
var liveQueryUuid = okResult.GetValue<Guid>(); | ||
|
||
var liveQuery = client.ListenLive<TestRecord>(liveQueryUuid); | ||
|
||
var cts = new CancellationTokenSource(); | ||
|
||
_ = Task.Run(async () => | ||
{ | ||
await foreach (var result in liveQuery.WithCancellation(cts.Token)) | ||
{ | ||
allResults.Add(result); | ||
} | ||
}); | ||
|
||
_ = Task.Run(async () => | ||
{ | ||
await WaitLiveQueryCreationAsync(); | ||
|
||
var record = await client.Create("test", new TestRecord { Value = 1 }); | ||
await WaitLiveQueryNotificationAsync(); | ||
|
||
await client.Upsert(new TestRecord { Id = record.Id, Value = 2 }); | ||
await WaitLiveQueryNotificationAsync(); | ||
|
||
await client.Delete(record.Id!); | ||
await WaitLiveQueryNotificationAsync(); | ||
|
||
await liveQuery.KillAsync(); | ||
await WaitLiveQueryNotificationAsync(); | ||
|
||
cts.Cancel(); | ||
}); | ||
|
||
await Task.Delay(Timeout); | ||
|
||
if (!cts.IsCancellationRequested) | ||
{ | ||
cts.Cancel(); | ||
throw new Exception("Timeout"); | ||
} | ||
}; | ||
|
||
await func.Should().NotThrowAsync(); | ||
|
||
allResults.Should().HaveCount(4); | ||
|
||
var firstResult = allResults[0]; | ||
firstResult.Should().BeOfType<SurrealDbLiveQueryCreateResponse<TestRecord>>(); | ||
|
||
var secondResult = allResults[1]; | ||
secondResult.Should().BeOfType<SurrealDbLiveQueryUpdateResponse<TestRecord>>(); | ||
|
||
var thirdResult = allResults[2]; | ||
thirdResult.Should().BeOfType<SurrealDbLiveQueryDeleteResponse>(); | ||
|
||
var lastResult = allResults[3]; | ||
lastResult.Should().BeOfType<SurrealDbLiveQueryCloseResponse>(); | ||
} | ||
} |
Oops, something went wrong.