I currently struggeling with the issue testing a TPL function inside of a RX statement. The problem is also that I don't have access to the observable.
Here is my test:
[Test]
public void TestRaceConditions()
{
// Arrange
TestScheduler testScheduler = new TestScheduler();
List<string> source = new List<string> {"Hello"};
List<string> results = new List<string>();
source.ToObservable().SelectMany(v => Task.Run(() =>
{
Thread.Sleep(10000); // Simulate long running operation
return v;
}).ToObservable(testScheduler))
.ObserveOn(testScheduler)
.Subscribe(s => results.Add(s));
// Act
testScheduler.Start();
Assert.That(results.Count, Is.EqualTo(1));
Assert.That(results[0], Is.EqualTo("Hello"));
}
I've googled around and found many articles and advices like:
- Do not mix TPL and RX
- Create a wrapper for the TPL code where you can mock and return a completed task. ==> But what about integration tests?
What I also tried is to create a custom TaskScheduler
, but then I have to replace every Task.Run
with Task.Factory.StartNew
.
So the question is:
- How to get control over the task or the observable in the test just with
Schedulers
that I know when all elements are processed and finished?
Aucun commentaire:
Enregistrer un commentaire