Skip to content

Commit

Permalink
Return and unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Dec 10, 2023
1 parent f9360d8 commit 400d079
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 53 deletions.
21 changes: 11 additions & 10 deletions sandbox/ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


SubscriptionTracker.EnableTracking = true;
SubscriptionTracker.EnableStackTrace = false;
SubscriptionTracker.EnableStackTrace = true;

using var factory = LoggerFactory.Create(x =>
{
Expand All @@ -36,7 +36,7 @@

SubscriptionTracker.ForEachActiveTask(x =>
{
// logger.ZLogInformation($"{x.TrackingId,3}: {Environment.NewLine}{x.StackTrace.Replace("R2.", "").Replace("C:\\MyGit\\R2\\sandbox\\ConsoleApp1\\", "").Replace("C:\\MyGit\\R2\\src\\R2\\", "")}");
logger.ZLogInformation($"{x.TrackingId,3}: {Environment.NewLine}{x.StackTrace.Replace("R2.", "").Replace("C:\\MyGit\\R2\\sandbox\\ConsoleApp1\\", "").Replace("C:\\MyGit\\R2\\src\\R2\\", "")}");


// logger.ZLogInformation($"{x.TrackingId,3}: {x.FormattedType}");
Expand All @@ -50,6 +50,7 @@



// System.Reactive.Linq.Observable.Empty<int>(

var s = new System.Reactive.Subjects.Subject<string>();

Expand All @@ -63,14 +64,14 @@



foreach (var item in typeof(System.Reactive.Linq.Observable).GetMethods().Select(x => x.Name).Distinct().OrderBy(x => x))
{
if (item == "ToString" || item == "Equals" || item == "GetHashCode" || item == "GetType")
{
continue;
}
Console.WriteLine("- [ ] " + item);
}
//foreach (var item in typeof(System.Reactive.Linq.Observable).GetMethods().Select(x => x.Name).Distinct().OrderBy(x => x))
//{
// if (item == "ToString" || item == "Equals" || item == "GetHashCode" || item == "GetType")
// {
// continue;
// }
// Console.WriteLine("- [ ] " + item);
//}



Expand Down
17 changes: 17 additions & 0 deletions src/R3/EventFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ namespace R3;

public static partial class EventFactory
{
// TODO: Empty, Never, Throw, Defer, DeferAsync, FromAsync, FromAsyncPattern, FromEvent, FromEventPattern, Interval, Range, Repeat, Start, Timer, Using, Create

// TODO: Convert
// ToArray
// ToAsync
// ToDictionary
// ToEnumerable
// ToEvent
// ToEventPattern
// ToList
// ToLookup
// ToObservable

// AsObservable
// AsSingleUnitObservable
// AsUnitObservable

public static CompletableEvent<int, Unit> Range(int start, int count)
{
return new Range(start, count);
Expand Down
4 changes: 1 addition & 3 deletions src/R3/Operators/Return.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using R3.Internal;

namespace R3
namespace R3
{
public static partial class EventFactory
{
Expand Down
13 changes: 10 additions & 3 deletions src/R3/SubscriptionTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using R3.Internal;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

Expand All @@ -12,9 +13,9 @@ public static class SubscriptionTracker
static int trackingIdCounter = 0;

// TODO: UnityEditor? AppSwitch?
public static bool EnableAutoReload = true;
public static bool EnableTracking = true;
public static bool EnableStackTrace = true;
// public static bool EnableAutoReload = false;
public static bool EnableTracking = false;
public static bool EnableStackTrace = false;
// TODO: EnableStackTraceFileLink

static readonly WeakDictionary<TrackableDisposable, TrackingState> tracking = new();
Expand All @@ -25,14 +26,19 @@ public static class SubscriptionTracker
// flag for polling performance
static bool dirty;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool TryTrackActiveSubscription(IDisposable subscription, int skipFrame, [NotNullWhen(true)] out TrackableDisposable? trackableDisposable)
{
if (!EnableTracking)
{
trackableDisposable = default;
return false;
}
return TryTrackActiveSubscriptionCore(subscription, skipFrame, out trackableDisposable);
}

internal static bool TryTrackActiveSubscriptionCore(IDisposable subscription, int skipFrame, [NotNullWhen(true)] out TrackableDisposable? trackableDisposable)
{
dirty = true;

string stackTrace = "";
Expand Down Expand Up @@ -63,6 +69,7 @@ internal static bool TryTrackActiveSubscription(IDisposable subscription, int sk
return true;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void RemoveTracking(TrackableDisposable subscription)
{
if (!EnableTracking) return;
Expand Down
48 changes: 48 additions & 0 deletions tests/R3.Tests/OperatorTests/ReturnTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.Extensions.Time.Testing;

namespace R3.Tests.OperatorTests;

public class ReturnTest
{
[Fact]
public void Return()
{
{
using var list = EventFactory.Return(10).LiveRecord();
list.AssertEqual([10]);
list.AssertIsCompleted();
}
{
var fakeTime = new FakeTimeProvider();

using var list = EventFactory.Return(10, TimeSpan.Zero, fakeTime).LiveRecord();
list.AssertEqual([10]);
list.AssertIsCompleted();
}
{
var fakeTime = new FakeTimeProvider();

using var list = EventFactory.Return(10, TimeSpan.FromSeconds(5), fakeTime).LiveRecord();
list.AssertEqual([]);

fakeTime.Advance(TimeSpan.FromSeconds(4));
list.AssertEqual([]);
list.AssertIsNotCompleted();

fakeTime.Advance(TimeSpan.FromSeconds(1));
list.AssertEqual([10]);
list.AssertIsCompleted();
}
}

[Fact]
public void ReturnThreadPoolScheduleOptimized()
{
using var list = EventFactory.Return(10).LiveRecord();

Thread.Sleep(1);

list.AssertEqual([10]);
list.AssertIsCompleted();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using FluentAssertions;

namespace R3.Tests;
namespace R3.Tests.OperatorTests;

public class WhereTest(ITestOutputHelper output)
{
Expand All @@ -12,16 +10,16 @@ public void Where()
using var list = p.Where(x => x % 2 != 0).LiveRecord();

p.PublishOnNext(2);
list.Should().BeEmpty();
list.AssertEqual([]);

p.PublishOnNext(1);
list.Should().Equal([1]);
list.AssertEqual([1]);

p.PublishOnNext(3);
list.Should().Equal([1, 3]);
list.AssertEqual([1, 3]);

p.PublishOnNext(30);
list.Should().Equal([1, 3]);
list.AssertEqual([1, 3]);
}

// test WhereWhere optimize
Expand All @@ -33,22 +31,22 @@ public void WhereWhere()
using var list = p.Where(x => x % 2 != 0).Where(x => x % 3 != 0).LiveRecord();

p.PublishOnNext(2);
list.Should().BeEmpty();
list.AssertEqual([]);

p.PublishOnNext(1);
list.Should().Equal([1]);
list.AssertEqual([1]);

p.PublishOnNext(3);
list.Should().Equal([1]);
list.AssertEqual([1]);

p.PublishOnNext(5);
list.Should().Equal([1, 5]);
list.AssertEqual([1, 5]);

p.PublishOnNext(6);
list.Should().Equal([1, 5]);
list.AssertEqual([1, 5]);

p.PublishOnNext(7);
list.Should().Equal([1, 5, 7]);
list.AssertEqual([1, 5, 7]);
}

//test where indexed
Expand All @@ -60,22 +58,22 @@ public void WhereIndexed()
using var list = p.Where((x, i) => i % 2 != 0).LiveRecord();

p.PublishOnNext(2);
list.Should().BeEmpty();
list.AssertEqual([]);

p.PublishOnNext(1);
list.Should().Equal([1]);
list.AssertEqual([1]);

p.PublishOnNext(3);
list.Should().Equal([1]);
list.AssertEqual([1]);

p.PublishOnNext(5);
list.Should().Equal([1, 5]);
list.AssertEqual([1, 5]);

p.PublishOnNext(6);
list.Should().Equal([1, 5]);
list.AssertEqual([1, 5]);

p.PublishOnNext(8);
list.Should().Equal([1, 5, 8]);
list.AssertEqual([1, 5, 8]);
}

// test where completable
Expand All @@ -87,22 +85,22 @@ public void WhereCompletable()
using var list = p.Where(x => x % 2 != 0).LiveRecord();

p.PublishOnNext(2);
list.Should().BeEmpty();
list.AssertEqual([]);

p.PublishOnNext(1);
list.Should().Equal([1]);
list.AssertEqual([1]);

p.PublishOnNext(3);
list.Should().Equal([1, 3]);
list.AssertEqual([1, 3]);

p.PublishOnNext(30);
list.Should().Equal([1, 3]);
list.AssertEqual([1, 3]);

list.IsCompleted.Should().BeFalse();
list.AssertIsNotCompleted();

p.PublishOnCompleted(default);

list.IsCompleted.Should().BeTrue();
list.AssertIsCompleted();
}


Expand All @@ -115,27 +113,27 @@ public void WhereCompletableIndexed()
using var list = p.Where((x, i) => i % 2 != 0).LiveRecord();

p.PublishOnNext(2);
list.Should().BeEmpty();
list.AssertEqual([]);

p.PublishOnNext(1);
list.Should().Equal([1]);
list.AssertEqual([1]);

p.PublishOnNext(3);
list.Should().Equal([1]);
list.AssertEqual([1]);

p.PublishOnNext(5);
list.Should().Equal([1, 5]);
list.AssertEqual([1, 5]);

p.PublishOnNext(6);
list.Should().Equal([1, 5]);
list.AssertEqual([1, 5]);

p.PublishOnNext(8);
list.Should().Equal([1, 5, 8]);
list.AssertEqual([1, 5, 8]);

list.IsCompleted.Should().BeFalse();
list.AssertIsNotCompleted();

p.PublishOnCompleted(default);

list.IsCompleted.Should().BeTrue();
list.AssertIsCompleted();
}
}
8 changes: 5 additions & 3 deletions tests/R3.Tests/R3.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="System.Reactive.Linq" Version="6.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -36,6 +37,7 @@
<Using Include="R3" />
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
<Using Include="FluentAssertions" />
</ItemGroup>

</Project>
21 changes: 20 additions & 1 deletion tests/R3.Tests/_TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace R3.Tests;
using FluentAssertions;

namespace R3.Tests;

public static class _TestHelper
{
Expand Down Expand Up @@ -26,4 +28,21 @@ public void Dispose()
{
SourceSubscription.Dispose();
}

// test helper

public void AssertIsCompleted()
{
IsCompleted.Should().BeTrue();
}

public void AssertIsNotCompleted()
{
IsCompleted.Should().BeFalse();
}

public void AssertEqual(params T[] expected)
{
this.Should().Equal(expected);
}
}

0 comments on commit 400d079

Please sign in to comment.