Skip to content

Instantly share code, notes, and snippets.

@danielmoore
Forked from ScottWeinstein/IsAlives.cs
Created May 29, 2011 07:05
Show Gist options
  • Save danielmoore/997536 to your computer and use it in GitHub Desktop.
Save danielmoore/997536 to your computer and use it in GitHub Desktop.
Rx IsAlive UnitTests
// doesn't detect Never()
public static IObservable<bool> IsAlive_Merge<T>(this IObservable<T> source, TimeSpan timeout, IScheduler sched)
{
return source.Select(_ => true)
.Merge(source.Select(_ => false).Throttle(timeout, sched))
.DistinctUntilChanged();
}
// D.Moore, 29 May: using the buffer count should cause this to fire as soon as it's available.
public static IObservable<bool> IsAlive_Buffer<T>(this IObservable<T> source, TimeSpan timeout, IScheduler sched)
{
return source.Buffer(timeout, 1, sched).Select(l => l.Any()).DistinctUntilChanged();
}
// via http://enumeratethis.com/2010/10/ doesn't detect Never()
public static IObservable<bool> IsAlive_Switch<T>(this IObservable<T> source, TimeSpan timeout, IScheduler sched)
{
return Observable.Switch(
from hb in source
select Observable.Return(true).Concat(Observable.Return(false).Delay(timeout, sched))
)
.DistinctUntilChanged();
}
// works, but isn't the most clear
public static IObservable<bool> IsAlive_Window<T>(this IObservable<T> source, TimeSpan timeout, IScheduler sched)
{
return source.Window(timeout, sched)
.Select(wind => wind.Any())
.SelectMany(a => a)
.DistinctUntilChanged();
}
[Fact]
public void IsAlive_Is_False_On_NeverStream()
{
var source = Observable.Never<Unit>();
var actual = _testScheduler.Start( () => source.IsAlive(TimeSpan.FromTicks(50), _testScheduler));
actual.Messages.AssertEqual(OnNext(250, false));
}
[Fact]
public void IsAlive_Is_Alive_On_Heartbeat()
{
var source = _testScheduler.CreateHotObservable(OnNext(600,1));
var actual = _testScheduler.Start(() => source.IsAlive(TimeSpan.FromTicks(500), _testScheduler));
actual.Messages.AssertEqual(OnNext(600, true));
}
[Fact]
public void IsAlive_Detects_FirstHeartbeatAfterTimeout()
{
var source = _testScheduler.CreateHotObservable(OnNext(901, 1));
var actual = _testScheduler.Start(() => source.IsAlive(TimeSpan.FromTicks(100), _testScheduler));
actual.Messages.AssertEqual(OnNext(300, false), OnNext(901, true));
}
[Fact]
public void IsAlive_Detects_StoppedHeartbeat()
{
var source = _testScheduler.CreateHotObservable(OnNext(300, 1));
var actual = _testScheduler.Start(() => source.IsAlive(TimeSpan.FromTicks(100), _testScheduler));
actual.Messages.AssertEqual(OnNext(300, true), OnNext(400, false));
}
[Fact]
public void IsAlive_Detects_RestartedHeartbeat()
{
var source = _testScheduler.CreateHotObservable(OnNext(300, 1), OnNext(901, 1));
var actual = _testScheduler.Start(() => source.IsAlive(TimeSpan.FromTicks(100), _testScheduler));
actual.Messages.AssertEqual(OnNext(300, true), OnNext(400, false), OnNext(901, true));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment