Skip to content

Commit

Permalink
Add support for running tests on BaaSaas locally (#3523)
Browse files Browse the repository at this point in the history
* Added various things

* Small fixes

* Fixed running baasaas locally

* Various improvements

* Clean up

* Small fixes

* Fix

* Docs fix

* Small fix

* Removed unity pragma

* Small fix

* Unification

* Corrections

* Added wait for baas to be up

* Fixes according to PR
  • Loading branch information
papafe authored Feb 21, 2024
1 parent 0d55730 commit a9a21f8
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Realm/Realm/Handles/SyncUserHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public async Task<string> CallFunctionAsync(AppHandle app, string name, string a
NativeMethods.call_function(this, app, name, name.IntPtrLength(), args, args.IntPtrLength(), service, service.IntPtrLength(), GCHandle.ToIntPtr(tcsHandle), out var ex);
ex.ThrowIfNecessary();

return await tcs.Task; //.NET Host locks the dll when there's an exception here (coming from native)
return await tcs.Task; // .NET Host locks the dll when there's an exception here (coming from native)
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion Realm/Realm/Sync/FlexibleSync/WaitForSyncMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public enum WaitForSyncMode
/// With this mode enabled, Realm will always return as soon as the the subscription is created
/// while any server data is being downloaded in the background. This update is not atomic, which
/// means that if you subscribe to notifications using
/// <see cref="CollectionExtensions.SubscribeForNotifications{T}(IQueryable{T}, NotificationCallbackDelegate{T})"/>
/// <see cref="CollectionExtensions.SubscribeForNotifications{T}(IQueryable{T}, NotificationCallbackDelegate{T}, KeyPathsCollection?)"/>
/// or <see cref="INotifyCollectionChanged.CollectionChanged"/>
/// you might see multiple events being fired as the server sends objects matching the subscription.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Tests/Realm.Tests/Realm.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Tools\DeployApps\BaasClient.cs">
<Link>Sync\BaasClient.cs</Link>
<Link>Sync\Baas\BaasClient.cs</Link>
<SubType></SubType>
</Compile>
</Compile>
</ItemGroup>
<ItemGroup>
<Folder Include="Generated\Realm.SourceGenerator\Realms.SourceGenerator.RealmGenerator\" />
Expand Down
2 changes: 1 addition & 1 deletion Tests/Realm.Tests/Sync/StaticQueriesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void RealmObjectAPI_Primitive_RealmToAtlas(TestCaseData<SyncAllTypesObjec

/* We need specific tests regarding datetime because of the different behaviour of the server and
* DateTimeOffset.ToUnixUniversalTime():
* - DateTimeOffset.ToUnixTimeMilliseconds always rounds to the “past”.So for dates after Unix epoch it’s the same as a truncation,
* - DateTimeOffset.ToUnixTimeMilliseconds always rounds to the “past”. So for dates after Unix epoch it’s the same as a truncation,
* for dates before this means that they subtract 1 millisecond if there’s any sub-millisecond digit (https://github.com/microsoft/referencesource/blob/51cf7850defa8a17d815b4700b67116e3fa283c2/mscorlib/system/datetimeoffset.cs#L666C11-L666C11)
* - The server truncates sub-milliseconds precision all times
* This means that there is an issue when going from Realm to Atlas and then deserializing from Atlas, as there will be a difference
Expand Down
54 changes: 27 additions & 27 deletions Tests/Realm.Tests/Sync/SyncTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,17 @@ public static class SyncTestHelpers

public static Uri? BaasUri;
private static BaasClient? _baasClient;
private static string? _baaSaasApiKey;

static SyncTestHelpers()
{
#if !UNITY
try
var uri = ConfigHelpers.GetSetting("BaasUrl");
if (uri != null)
{
var uri = ConfigHelpers.GetSetting("BaasUrl");
if (uri != null)
{
BaasUri = new Uri(uri);
}
BaasUri = new Uri(uri);
}
catch
{
}
#endif

_baaSaasApiKey = ConfigHelpers.GetSetting("BaaSaasApiKey");
}

private static int _appCounter;
Expand All @@ -76,7 +71,7 @@ static SyncTestHelpers()

public static void RunBaasTestAsync(Func<Task> testFunc, int timeout = 30000)
{
if (BaasUri == null)
if (BaasUri == null && _baaSaasApiKey == null)
{
Assert.Ignore("Atlas App Services are not setup.");
}
Expand Down Expand Up @@ -132,7 +127,7 @@ public static async Task<string[]> ExtractBaasSettingsAsync(string[] args)

public static (string[] RemainingArgs, IDisposable? Logger) SetLoggerFromArgs(string[] args)
{
var (extracted, remaining) = ArgumentHelper.ExtractArguments(args, "realmloglevel", "realmlogfile");
var (extracted, remaining) = BaasClient.ExtractArguments(args, "realmloglevel", "realmlogfile");

if (extracted.TryGetValue("realmloglevel", out var logLevelStr) && Enum.TryParse<LogLevel>(logLevelStr, out var logLevel))
{
Expand Down Expand Up @@ -167,28 +162,33 @@ public static (string[] RemainingArgs, IDisposable? Logger) SetLoggerFromArgs(st

private static async Task CreateBaasAppsAsync()
{
if (_apps[AppConfigType.Default].AppId != string.Empty || BaasUri == null)
if (_apps[AppConfigType.Default].AppId != string.Empty || (BaasUri == null && _baaSaasApiKey == null))
{
return;
}

#if !UNITY
try
{
var cluster = ConfigHelpers.GetSetting("Cluster")!;
var apiKey = ConfigHelpers.GetSetting("ApiKey")!;
var privateApiKey = ConfigHelpers.GetSetting("PrivateApiKey")!;
var groupId = ConfigHelpers.GetSetting("GroupId")!;
var differentiator = ConfigHelpers.GetSetting("Differentiator") ?? "local";
var cluster = ConfigHelpers.GetSetting("Cluster")!;
var apiKey = ConfigHelpers.GetSetting("ApiKey")!;
var privateApiKey = ConfigHelpers.GetSetting("PrivateApiKey")!;
var groupId = ConfigHelpers.GetSetting("GroupId")!;
var differentiator = ConfigHelpers.GetSetting("Differentiator") ?? "local";

_baasClient ??= await BaasClient.Atlas(BaasUri, differentiator, TestHelpers.Output, cluster, apiKey, privateApiKey, groupId);
if (_baaSaasApiKey != null)
{
BaasUri = await BaasClient.GetOrDeployContainer(_baaSaasApiKey, differentiator, TestHelpers.Output);
_baasClient = await BaasClient.Docker(BaasUri, differentiator, TestHelpers.Output);
}
catch
else if (!string.IsNullOrEmpty(cluster) &&
!string.IsNullOrEmpty(apiKey) &&
!string.IsNullOrEmpty(privateApiKey) &&
!string.IsNullOrEmpty(groupId))
{
_baasClient = await BaasClient.Atlas(BaasUri!, differentiator, TestHelpers.Output, cluster, apiKey, privateApiKey, groupId);
}
else
{
_baasClient = await BaasClient.Docker(BaasUri!, "local", TestHelpers.Output);
}
#endif

_baasClient ??= await BaasClient.Docker(BaasUri, "local", TestHelpers.Output);

_apps = await _baasClient.GetOrCreateApps();
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a9a21f8

Please sign in to comment.