test_manage.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "catch.hpp"
  2. #include "shm_manager.h"
  3. #include "shm.h"
  4. #include "shm_object.h"
  5. extern bg::detail::ShmContext g_shm_ctx;
  6. TEST_CASE("ShmManager, AllocateRawMemory")
  7. {
  8. bg::ShmOptions opts(false, "Test", nullptr);
  9. opts.fix_vptr_on_init = false;
  10. REQUIRE(bg::ShmInit(opts) == true);
  11. REQUIRE(g_shm_ctx.mgr);
  12. for(size_t i = 8; i < 0x100000000LL; i = i << 4)
  13. {
  14. size_t real_bytes = i - 1;
  15. void* memory = g_shm_ctx.mgr->AllocateRawMemory(&real_bytes, i);
  16. REQUIRE(memory);
  17. memset(memory, 0, real_bytes);
  18. REQUIRE(real_bytes == i);
  19. }
  20. bg::ShmFini();
  21. }
  22. struct TestSigleton
  23. {
  24. TestSigleton(int a, char b, void* c)
  25. {
  26. this->a = a;
  27. this->b = b;
  28. this->c = c;
  29. }
  30. int a{};
  31. char b{};
  32. void* c{};
  33. };
  34. TEST_CASE("ShmManager, Singleton")
  35. {
  36. bg::ShmOptions opts(false, "Test", nullptr);
  37. opts.fix_vptr_on_init = false;
  38. REQUIRE(bg::ShmInit(opts) == true);
  39. REQUIRE(g_shm_ctx.mgr);
  40. bg::detail::TypeName type(typeid(TestSigleton).name(), sizeof(TestSigleton));
  41. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
  42. int a = 3;
  43. REQUIRE(bg::ShmGetSingleton<TestSigleton>(1, 2, &a));
  44. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == true);
  45. TestSigleton* temp = bg::ShmGetSingleton<TestSigleton>(1, 1, &a);
  46. REQUIRE(temp->a == 1);
  47. REQUIRE(temp->b == 2);
  48. REQUIRE(temp->c == &a);
  49. bg::ShmDeleteSingleton<TestSigleton>();
  50. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
  51. bg::ShmFini();
  52. }
  53. TEST_CASE("ShmManager, creat/resume")
  54. {
  55. char path[256];
  56. bg::ShmOptions opts(false, "Test", nullptr);
  57. opts.fix_vptr_on_init = true;
  58. REQUIRE(bg::ShmInit(opts) == true);
  59. int a = 3;
  60. REQUIRE(g_shm_ctx.mgr);
  61. bg::detail::TypeName type(typeid(TestSigleton).name(), sizeof(TestSigleton));
  62. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
  63. REQUIRE(bg::ShmGetSingleton<TestSigleton>(1, 2, &a));
  64. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == true);
  65. opts.resume = true;
  66. // 占住共享内存 windows 会自动释放
  67. size_t real_size = sizeof(bg::detail::ShmManager);
  68. snprintf(path, 256, "%s-mgr.mmap", opts.identifier);
  69. void* memory1 = bg::detail::ShmObjectAttach(path, 0, &real_size, &real_size);
  70. REQUIRE(memory1);
  71. bg::detail::ShmObjectDelete(g_shm_ctx.mgr, real_size, path);
  72. real_size = 0x100000000;
  73. void* memory2 = bg::detail::ShmObjectAttach("Test-000.mmap", 0, &real_size, &real_size);
  74. REQUIRE(memory2);
  75. bg::detail::ShmObjectDelete((void*)0x00006f0000000000, real_size, "Test-000.mmap");
  76. g_shm_ctx.mgr = nullptr;
  77. REQUIRE(bg::ShmInit(opts) == true);
  78. bg::detail::ShmObjectDelete(memory1, sizeof(bg::detail::ShmManager), path);
  79. bg::detail::ShmObjectDelete(memory2, real_size, "Test-000.mmap");
  80. TestSigleton* temp = bg::ShmGetSingleton<TestSigleton>(1, 1, &a);
  81. REQUIRE(temp->a == 1);
  82. REQUIRE(temp->b == 2);
  83. REQUIRE(temp->c == &a);
  84. bg::ShmDeleteSingleton<TestSigleton>();
  85. REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
  86. bg::ShmFini();
  87. }