我在下面的单元测试中尝试从一个可以通过标识符观察到的源中对对象进行分组,并且每隔50个刻度对它们进行采样。但是,带有GroupBy
,SelectMany
-〉Sample
的代码给出了与预期输出不同的输出。它返回OnNext@280、OnNext@380和OnCompleted@380。
当我只使用Sample
方法运行测试时,输出符合预期,如AssertEqual
调用中所示。
我在这里做错了什么?我希望两个版本应该产生相同的输出,因为在本例中,所有产品的标识符都是相同的。
Ps:我已经检查了this question about the same kind of operation和this question on grouped throttling+无功源单元测试。
public class ObservableTests : ReactiveTest
{
[Fact]
public void sample_test()
{
// Arrange
var productPrice = new ProductPrice("1", 10);
var productPrice2 = new ProductPrice("1", 20);
var productPrice3 = new ProductPrice("1", 30);
var productPrice4 = new ProductPrice("1", 40);
var productPrice5 = new ProductPrice("1", 50);
var productPrice6 = new ProductPrice("1", 60);
var testScheduler = new TestScheduler();
var observable = testScheduler.CreateHotObservable(
OnNext(230, productPrice),
OnNext(260, productPrice2),
OnNext(280, productPrice3),
OnNext(340, productPrice4),
OnNext(360, productPrice5),
OnNext(380, productPrice6),
OnCompleted<ProductPrice>(380));
// Act
// WORKS
//var result = testScheduler.Start(
// () =>
// observable
// .Sample(TimeSpan.FromTicks(50), testScheduler));
// DOESNT WORK
var result = testScheduler.Start(
() =>
observable
.GroupBy(value => value.Identifier)
.SelectMany(groupedObservable => groupedObservable.Sample(TimeSpan.FromTicks(50), testScheduler)));
result.Messages.AssertEqual(
OnNext(250, productPrice),
OnNext(300, productPrice3),
OnNext(350, productPrice4),
OnNext(400, productPrice6),
OnCompleted<ProductPrice>(400));
}
private class ProductPrice
{
public ProductPrice(string identifier, decimal price)
{
this.Identifier = identifier;
this.Price = price;
}
public string Identifier { get; }
public decimal Price { get; }
}
1条答案
按热度按时间4urapxun1#
这是一个迟来的答案,但它的工作完全符合预期。只有在发出第一个元素后,才会创建和订阅
groupedObservable
--在230 tick。因此,从该时间点开始,采样将在该组中开始,在280、330、380等处给出结果...在没有分组的工作示例中,可观察对象在tick 100处创建,在tick 200处订阅(
TestScheduler.Start
方法的此重载的默认设置),因此第一个采样元素在tick 250处发出。