/test/SlowTests/Issues/RavenDb1962.cs

https://github.com/fitzchak/ravendb
C# | 116 lines | 101 code | 15 blank | 0 comment | 4 complexity | 1fd3b2447156eeb030368e9f32368933 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using FastTests;
  6. using Raven.Client.Documents;
  7. using Raven.Client.Documents.Session;
  8. using SlowTests.Core.Utils.Entities;
  9. using Xunit;
  10. namespace SlowTests.Issues
  11. {
  12. public class RavenDb1962 : RavenTestBase
  13. {
  14. private const int Cntr = 5;
  15. [Fact]
  16. public async Task CanExecuteLazyLoadsInAsyncSession()
  17. {
  18. using (var store = GetDocumentStore())
  19. {
  20. using (var session = store.OpenAsyncSession())
  21. {
  22. await StoreDataAsync(store, session);
  23. var userFetchTasks = LazyLoadAsync(store, session);
  24. var i = 1;
  25. foreach (var lazy in userFetchTasks)
  26. {
  27. var user = await lazy.Value;
  28. Assert.Equal(user.Name, "Test User #" + i);
  29. i++;
  30. }
  31. }
  32. }
  33. }
  34. [Fact]
  35. public async Task CanExecuteLazyLoadsInAsyncSession_CheckSingleCall()
  36. {
  37. using (var store = GetDocumentStore())
  38. {
  39. using (var session = store.OpenAsyncSession())
  40. {
  41. await StoreDataAsync(store, session);
  42. }
  43. using (var session = store.OpenAsyncSession())
  44. {
  45. LazyLoadAsync(store, session);
  46. var requestTimes = await session.Advanced.Eagerly.ExecuteAllPendingLazyOperationsAsync();
  47. Assert.Equal(1, session.Advanced.NumberOfRequests);
  48. Assert.NotNull(requestTimes.TotalClientDuration);
  49. Assert.NotNull(requestTimes.TotalServerDuration);
  50. Assert.Equal(Cntr, requestTimes.DurationBreakdown.Count);
  51. }
  52. }
  53. }
  54. [Fact]
  55. public async Task CanExecuteLazyQueriesInAsyncSession()
  56. {
  57. using (var store = GetDocumentStore())
  58. {
  59. using (var session = store.OpenAsyncSession())
  60. {
  61. await StoreDataAsync(store, session);
  62. }
  63. WaitForIndexing(store);
  64. using (var session = store.OpenAsyncSession())
  65. {
  66. var q1 = session.Query<User>()
  67. .Customize(x => x.WaitForNonStaleResults())
  68. .Where(x => x.Name == "Test User #1").LazilyAsync();
  69. var q2 = session.Query<User>()
  70. .Customize(x => x.WaitForNonStaleResults())
  71. .Where(x => x.Name == "Test User #3").LazilyAsync();
  72. var requestTimes = await session.Advanced.Eagerly.ExecuteAllPendingLazyOperationsAsync();
  73. Assert.NotNull(requestTimes.TotalClientDuration);
  74. Assert.NotNull(requestTimes.TotalServerDuration);
  75. Assert.Equal(2, requestTimes.DurationBreakdown.Count);
  76. Assert.Equal(1, (await q1.Value).Count());
  77. Assert.Equal(1, (await q2.Value).Count());
  78. Assert.Equal(1, session.Advanced.NumberOfRequests);
  79. }
  80. }
  81. }
  82. private async Task StoreDataAsync(DocumentStore store, IAsyncDocumentSession session)
  83. {
  84. for (var i = 1; i <= Cntr; i++)
  85. {
  86. await session.StoreAsync(new User { Name = "Test User #" + i }, "users/" + i);
  87. }
  88. await session.SaveChangesAsync();
  89. }
  90. private List<Lazy<Task<User>>> LazyLoadAsync(DocumentStore store, IAsyncDocumentSession session)
  91. {
  92. var listTasks = new List<Lazy<Task<User>>>();
  93. for (var i = 1; i <= Cntr; i++)
  94. {
  95. var userFetchTask = session.Advanced.Lazily.LoadAsync<User>("users/" + i);
  96. listTasks.Add(userFetchTask);
  97. }
  98. return listTasks;
  99. }
  100. }
  101. }