Guest User

Untitled

a guest
Jul 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. public class MyCoolViewModel: ReactiveObject
  2. {
  3. // Create a Property to store the results
  4. ObservableAsPropertyHelper<byte[]> _BytesWeHaveRead;
  5. public byte[] BytesWeHaveRead {
  6. get { return _BytesWeHaveRead.Value; }
  7. }
  8.  
  9. // The command we'll be testing
  10. ReactiveAsyncCommand ReadBytesCommand { get; private set; }
  11.  
  12. public MyCoolViewModel(Func<IObservable<byte[]>> readBytesFunc)
  13. {
  14. ReadBytesCommand = new ReactiveAsyncCommand();
  15.  
  16. // Take our Command, send it through the readBytesFunc function, then
  17. // pipe the results to the BytesWeHaveRead property
  18. _BytesWeHaveRead = ReadBytesCommand
  19. .RegisterAsyncObservable(_ => readBytesFunc())
  20. .ToProperty(this, x => x.BytesWeHaveRead);
  21. }
  22. }
  23.  
  24. IObservable<byte[]> mockReadBytesAsync()
  25. {
  26. // Wait ten seconds, then return the byte array
  27. return Observable.Return(new byte[] {1,2,3}).Delay(TimeSpan.FromSeconds(10), RxApp.TaskpoolScheduler);
  28. }
  29.  
  30. [Fact]
  31. public void ReadBytesAsyncCommandTest()
  32. {
  33. // Replace all schedulers with the TestScheduler
  34. (new TestScheduler()).With(sched => {
  35. var fixture = new MyCoolViewModel(mockReadBytesAsync);
  36.  
  37. // Execute the command - remember that it should take 10 seconds to
  38. // execute before returning
  39. fixture.ReadBytesCommand.Execute(null);
  40.  
  41. // 1 second in, it should still be running, the results should be empty
  42. sched.RunToMilliseconds(1000)
  43. Assert.False(fixture.ReadBytesCommand.CanExecute(null));
  44. Assert.Null(fixture.BytesWeHaveRead);
  45.  
  46. // 9 seconds in, same deal. Remember though, this doesn't take 9 seconds
  47. // of actual wall time to execute, this entire test is finished instantly
  48. sched.RunToMilliseconds(9000)
  49. Assert.False(fixture.ReadBytesCommand.CanExecute(null));
  50. Assert.Null(fixture.BytesWeHaveRead);
  51.  
  52. // 11 seconds in, it should be complete - we should be able to read
  53. // another block since we're done with the first one.
  54. sched.RunToMilliseconds(11000)
  55. Assert.True(fixture.ReadBytesCommand.CanExecute(null));
  56. Assert.Equal(1, fixture.BytesWeHaveRead[0]);
  57. Assert.Equal(2, fixture.BytesWeHaveRead[1]);
  58. Assert.Equal(3, fixture.BytesWeHaveRead[2]);
  59. });
  60. }
Add Comment
Please, Sign In to add comment