2
0

unit-ResourceManager.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <catch2/catch.hpp>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <gul/ResourceManager.h>
  5. #include <thread>
  6. struct TextResource
  7. {
  8. std::string data;
  9. };
  10. struct IntResource
  11. {
  12. uint64_t data = 0;
  13. };
  14. SCENARIO("Test")
  15. {
  16. using namespace gul;
  17. SingleResourceManager<TextResource> RM;
  18. const auto RES_URI = uri("file://" CMAKE_SOURCE_DIR "/README.md");
  19. auto rId = RM.findResource( RES_URI);
  20. REQUIRE( !rId->isLoaded() );
  21. REQUIRE( rId->getUri().toString() == RES_URI.toString());
  22. WHEN("We set the loader")
  23. {
  24. RM.setLoader([](uri const & _uri)
  25. {
  26. std::ifstream t(_uri.path);
  27. TextResource R;
  28. R.data = std::string((std::istreambuf_iterator<char>(t)),
  29. std::istreambuf_iterator<char>());
  30. return R;
  31. });
  32. THEN("We can load the resource")
  33. {
  34. rId->load();
  35. REQUIRE( rId->isLoaded() );
  36. REQUIRE( rId->get().data.size() > 0 );
  37. }
  38. THEN("We can load the resource")
  39. {
  40. REQUIRE( !rId->isLoaded());
  41. auto rId2 = RM.get(RES_URI);
  42. REQUIRE( rId2 == rId);
  43. REQUIRE( rId->isLoaded());
  44. REQUIRE( rId->get().data.size() > 0 );
  45. }
  46. THEN("We can load the resource from the handle")
  47. {
  48. auto & G = rId->get();
  49. REQUIRE( G.data.size() > 0);
  50. }
  51. }
  52. }
  53. SCENARIO("Background loading")
  54. {
  55. using namespace gul;
  56. SingleResourceManager<TextResource> RM;
  57. const auto RES_URI = uri("file://" CMAKE_SOURCE_DIR "/README.md");
  58. auto rId = RM.findResource( RES_URI);
  59. REQUIRE( !rId->isLoaded() );
  60. REQUIRE( rId->getUri().toString() == RES_URI.toString());
  61. RM.setLoader([](uri const & _uri)
  62. {
  63. std::ifstream t(_uri.path);
  64. TextResource R;
  65. R.data = std::string((std::istreambuf_iterator<char>(t)),
  66. std::istreambuf_iterator<char>());
  67. // simulate loading a large file
  68. std::this_thread::sleep_for(std::chrono::seconds(2));
  69. return R;
  70. });
  71. THEN("We can check if the resources is loaded")
  72. {
  73. REQUIRE( !rId->isLoaded() );
  74. REQUIRE( !rId->isLoading() ); // not loding in teh background
  75. // Get the background loader and pass it into a thread
  76. std::thread th(rId->getBackgroundLoader() );
  77. // wait a few moments so that the thread can actually start up
  78. std::this_thread::sleep_for( std::chrono::milliseconds(16));
  79. // loading in the background
  80. REQUIRE(rId->isLoading());
  81. // wait until its finished loading
  82. while( !rId->isLoaded())
  83. {
  84. std::this_thread::sleep_for( std::chrono::milliseconds(16));
  85. }
  86. th.join();
  87. // data should be fully loaded now
  88. REQUIRE( rId->isLoaded() );
  89. REQUIRE( !rId->isLoading());
  90. REQUIRE( rId->get().data.size() > 0);
  91. }
  92. }
  93. SCENARIO("Resource Manager")
  94. {
  95. using namespace gul;
  96. ResourceManager RM;
  97. RM.setLoader<TextResource>([](uri const & _uri)
  98. {
  99. std::ifstream t(_uri.path);
  100. TextResource R;
  101. R.data = std::string((std::istreambuf_iterator<std::string::value_type>(t)),
  102. std::istreambuf_iterator<std::string::value_type>());
  103. return R;
  104. });
  105. RM.setLoader<IntResource>([](uri const & _uri)
  106. {
  107. std::ifstream t(_uri.path);
  108. IntResource R;
  109. std::hash<std::string> H;
  110. R.data = H(_uri.toString());
  111. return R;
  112. });
  113. const auto RES_URI = uri("file://" CMAKE_SOURCE_DIR "/README.md");
  114. auto rId = RM.findResource<TextResource>( RES_URI);
  115. REQUIRE( !rId->isLoaded() );
  116. REQUIRE( rId->getUri().toString() == RES_URI.toString());
  117. THEN("We can immediately get the resource and load it")
  118. {
  119. auto tr = RM.get<TextResource>(RES_URI);
  120. REQUIRE( tr->isLoaded());
  121. REQUIRE(tr->get().data.size() > 0);
  122. auto ir = RM.get<IntResource>(RES_URI);
  123. REQUIRE( ir->isLoaded() );
  124. REQUIRE(ir->get().data > 0);
  125. }
  126. WHEN("We set the loader")
  127. {
  128. THEN("We can load the resource")
  129. {
  130. rId->load();
  131. REQUIRE( rId->isLoaded() );
  132. REQUIRE( rId->get().data.size() > 0 );
  133. }
  134. THEN("We can load the resource from the handle")
  135. {
  136. auto & G = rId->get();
  137. REQUIRE( G.data.size() > 0);
  138. }
  139. }
  140. }