public void Test2() { var result = new List <TraceRecord <int> >(); var subject = new Subject <int>(); var stream = subject .Trace("trace1", Observer.Create <TraceRecord <int> >(x => result.Add(x), error => { })); var subsc1 = stream.Subscribe(_ => { }); var subsc2 = stream.Subscribe(_ => { }); foreach (var i in Enumerable.Range(0, 3)) { subject.OnNext(i); Thread.Sleep(1000); } var comparer = new TraceRecordFuzzyComparer <int>(TimeSpan.FromSeconds(0.1), 0.01, true, null); var threadId = Thread.CurrentThread.ManagedThreadId; var res1_entity = result.Where(x => x.SubscriptionIndex == 0).OrderBy(x => x.TotalDuration).ToList(); var res2_entity = result.Where(x => x.SubscriptionIndex == 1).OrderBy(x => x.TotalDuration).ToList(); Assert.Equal(result.Count, res1_entity.Count + res2_entity.Count); Assert.Equal(new[] { new TraceRecord <int>("trace1", 0, 0, "", "", threadId, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0), 0), new TraceRecord <int>("trace1", 0, 1, "", "", threadId, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), 1), new TraceRecord <int>("trace1", 0, 2, "", "", threadId, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(1), 2), }, res1_entity, comparer); Assert.Equal(new[] { new TraceRecord <int>("trace1", 1, 0, "", "", threadId, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0), 0), new TraceRecord <int>("trace1", 1, 1, "", "", threadId, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), 1), new TraceRecord <int>("trace1", 1, 2, "", "", threadId, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(1), 2), }, res2_entity, comparer); }
public void Test3() { var result1 = new List <TraceRecord <int> >(); var result2 = new List <TraceRecord <int> >(); var subject = new Subject <int>(); var stream = subject .Trace("trace1", Observer.Create <TraceRecord <int> >(x => result1.Add(x), error => { })) .Where(x => x % 2 == 0) .Trace("trace2", Observer.Create <TraceRecord <int> >(x => result2.Add(x), error => { })); var subsc = stream.Subscribe(_ => { }); foreach (var i in Enumerable.Range(0, 5)) { subject.OnNext(i); Thread.Sleep(1000); } var comparer = new TraceRecordFuzzyComparer <int>(TimeSpan.FromSeconds(0.1), 0.01, true, null); var threadId = Thread.CurrentThread.ManagedThreadId; Assert.Equal(new[] { new TraceRecord <int>("trace1", 0, 0, "", "", threadId, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0), 0), new TraceRecord <int>("trace1", 0, 1, "", "", threadId, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), 1), new TraceRecord <int>("trace1", 0, 2, "", "", threadId, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(1), 2), new TraceRecord <int>("trace1", 0, 3, "", "", threadId, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(1), 3), new TraceRecord <int>("trace1", 0, 4, "", "", threadId, TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(1), 4), }, result1, comparer); Assert.Equal(new[] { new TraceRecord <int>("trace2", 0, 0, "", "", threadId, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0), 0), new TraceRecord <int>("trace2", 0, 1, "", "", threadId, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2), 2), new TraceRecord <int>("trace2", 0, 2, "", "", threadId, TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(2), 4), }, result2, comparer); }
public void Test4() { void waitWhile(Func <bool> isBlocked) { while (isBlocked()) { Thread.Sleep(1); } } var scheduler = new EventLoopScheduler(); var threadId = -1; scheduler.Schedule(() => threadId = Thread.CurrentThread.ManagedThreadId); waitWhile(() => threadId < 0); var result = new List <TraceRecord <int> >(); var subject = new Subject <int>(); var stream = subject .ObserveOn(scheduler) .Trace("trace1", Observer.Create <TraceRecord <int> >(x => result.Add(x), error => { })); var count = 0; var subsc = stream.Subscribe(_ => Interlocked.Increment(ref count)); foreach (var i in Enumerable.Range(0, 5)) { subject.OnNext(i); Thread.Sleep(1000); } waitWhile(() => count < 5); var comparer = new TraceRecordFuzzyComparer <int>(TimeSpan.FromSeconds(0.1), 0.01, true, null); Assert.Equal(new[] { new TraceRecord <int>("trace1", 0, 0, "", "", threadId, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0), 0), new TraceRecord <int>("trace1", 0, 1, "", "", threadId, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), 1), new TraceRecord <int>("trace1", 0, 2, "", "", threadId, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(1), 2), new TraceRecord <int>("trace1", 0, 3, "", "", threadId, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(1), 3), new TraceRecord <int>("trace1", 0, 4, "", "", threadId, TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(1), 4), }, result, comparer); }