Skip to content

Commit

Permalink
Add a test for verifying sync log level changes (#3389)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev authored Jul 21, 2023
1 parent f55dfd5 commit 0319c7f
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion Tests/Realm.Tests/Sync/SynchronizedInstanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
Expand All @@ -28,7 +30,6 @@
using Realms.Sync;
using Realms.Sync.ErrorHandling;
using Realms.Sync.Exceptions;

using NUnitExplicit = NUnit.Framework.ExplicitAttribute;

namespace Realms.Tests.Sync
Expand Down Expand Up @@ -728,6 +729,54 @@ public void CancelAsyncOperationsOnNonFatalErrors_WhenFalse_ShouldNotCancelAsync
});
}

[Test]
public void SyncLogger_WhenLevelChanges_LogsAtNewLevel()
{
var logs = new Dictionary<LogLevel, List<string>>();
foreach (LogLevel level in Enum.GetValues(typeof(LogLevel)))
{
logs[level] = new();
}

var regex = new Regex("Connection\\[\\d+]: Session\\[\\d+]");
var logger = Logger.Function((level, msg) =>
{
if (regex.IsMatch(msg))
{
logs[level].Add(msg);
}
});

Logger.LogLevel = LogLevel.Info;
Logger.Default = logger;

SyncTestHelpers.RunBaasTestAsync(async () =>
{
var config = await GetIntegrationConfigAsync();
using var realm = await GetRealmAsync(config);
var initialInfoLogs = logs[LogLevel.Info].Count;
Assert.That(initialInfoLogs, Is.GreaterThan(0));
Assert.That(logs[LogLevel.Debug].Count, Is.EqualTo(0));
Logger.LogLevel = LogLevel.Debug;
realm.Write(() =>
{
realm.Add(new PrimaryKeyStringObject
{
Id = Guid.NewGuid().ToString(),
});
});
await WaitForUploadAsync(realm);
Assert.That(logs[LogLevel.Info].Count, Is.GreaterThan(0));
Assert.That(logs[LogLevel.Debug].Count, Is.GreaterThan(0));
});
}

private const int DummyDataSize = 100;

private static void AddDummyData(Realm realm, bool singleTransaction)
Expand Down

0 comments on commit 0319c7f

Please sign in to comment.