diff --git a/libdmabufheap/test/unittest/libdmabufheap/dmabuf_alloc_test.cpp b/libdmabufheap/test/unittest/libdmabufheap/dmabuf_alloc_test.cpp index a47c608e225e57b63f2576b31a539e906998c08a..e8c36d5f971c0ab8905fa142919a9961e13483f6 100644 --- a/libdmabufheap/test/unittest/libdmabufheap/dmabuf_alloc_test.cpp +++ b/libdmabufheap/test/unittest/libdmabufheap/dmabuf_alloc_test.cpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include "securec.h" #include "gtest/gtest.h" #include "dmabuf_alloc.h" @@ -39,6 +41,7 @@ public: static void TearDownTestCase(); void SetUp(); void TearDown(); + std::string heapName; }; void DmabufAllocTest::SetUpTestCase() @@ -51,6 +54,21 @@ void DmabufAllocTest::TearDownTestCase() void DmabufAllocTest::SetUp() { + std::string rootDir = "/dev/dma_heap/"; + DIR *dir = opendir(rootDir.c_str()); + if (dir == nullptr) { + return; + } + struct dirent *ptr; + while ((ptr = readdir(dir)) != nullptr) { + std::string fileName = ptr->d_name; + std::string::size_type idx = fileName.find("system"); + if (idx != std::string::npos) { + heapName = fileName; + break; + } + } + closedir(dir); } void DmabufAllocTest::TearDown() @@ -59,7 +77,9 @@ void DmabufAllocTest::TearDown() HWTEST_F(DmabufAllocTest, AllocSingleBuffer, TestSize.Level1) { - int heapFd = DmabufHeapOpen("system"); + ASSERT_STRNE(heapName.c_str(), ""); + + int heapFd = DmabufHeapOpen(heapName.c_str()); ASSERT_GE(heapFd, 0); DmabufHeapBuffer buffer = { .size = BUFFER_SIZE, .heapFlags = 0 }; @@ -85,7 +105,9 @@ HWTEST_F(DmabufAllocTest, AllocSingleBuffer, TestSize.Level1) HWTEST_F(DmabufAllocTest, ShareBufferBetweenProcess, Function|MediumTest|Level1) { - int heapFd = DmabufHeapOpen("system"); + ASSERT_STRNE(heapName.c_str(), ""); + + int heapFd = DmabufHeapOpen(heapName.c_str()); ASSERT_GE(heapFd, 0); DmabufHeapBuffer buffer = { .size = BUFFER_SIZE, .heapFlags = 0 }; @@ -153,7 +175,9 @@ HWTEST_F(DmabufAllocTest, OpenInvalidNameHeap, Function|MediumTest|Level1) HWTEST_F(DmabufAllocTest, AllocInvalidSizeBuffer, Function|MediumTest|Level1) { - int heapFd = DmabufHeapOpen("system"); + ASSERT_STRNE(heapName.c_str(), ""); + + int heapFd = DmabufHeapOpen(heapName.c_str()); ASSERT_GE(heapFd, 0); DmabufHeapBuffer buffer = { .size = 0 }; @@ -164,9 +188,11 @@ HWTEST_F(DmabufAllocTest, AllocInvalidSizeBuffer, Function|MediumTest|Level1) HWTEST_F(DmabufAllocTest, BufferSyncWithWrongFd, Function|MediumTest|Level1) { + ASSERT_STRNE(heapName.c_str(), ""); + const unsigned int WRONG_FD = UINT_MAX; - int heapFd = DmabufHeapOpen("system"); + int heapFd = DmabufHeapOpen(heapName.c_str()); ASSERT_GE(heapFd, 0); DmabufHeapBuffer buffer = { .size = BUFFER_SIZE, .heapFlags = 0 }; @@ -188,9 +214,11 @@ HWTEST_F(DmabufAllocTest, BufferSyncWithWrongFd, Function|MediumTest|Level1) HWTEST_F(DmabufAllocTest, BufferSyncWithWrongSyncType, Function|MediumTest|Level1) { + ASSERT_STRNE(heapName.c_str(), ""); + const unsigned int WRONG_SYNC_TYPE = UINT_MAX; - int heapFd = DmabufHeapOpen("system"); + int heapFd = DmabufHeapOpen(heapName.c_str()); ASSERT_GE(heapFd, 0); DmabufHeapBuffer buffer = { .size = BUFFER_SIZE, .heapFlags = 0 }; @@ -212,7 +240,9 @@ HWTEST_F(DmabufAllocTest, BufferSyncWithWrongSyncType, Function|MediumTest|Level HWTEST_F(DmabufAllocTest, SyncBufferTwice, Function|MediumTest|Level1) { - int heapFd = DmabufHeapOpen("system"); + ASSERT_STRNE(heapName.c_str(), ""); + + int heapFd = DmabufHeapOpen(heapName.c_str()); ASSERT_GE(heapFd, 0); DmabufHeapBuffer buffer = { .size = BUFFER_SIZE, .heapFlags = 0 }; @@ -242,7 +272,9 @@ HWTEST_F(DmabufAllocTest, SyncBufferTwice, Function|MediumTest|Level1) HWTEST_F(DmabufAllocTest, ExchangeBufferSyncOrder, Function|MediumTest|Level1) { - int heapFd = DmabufHeapOpen("system"); + ASSERT_STRNE(heapName.c_str(), ""); + + int heapFd = DmabufHeapOpen(heapName.c_str()); ASSERT_GE(heapFd, 0); DmabufHeapBuffer buffer = { .size = BUFFER_SIZE, .heapFlags = 0 }; diff --git a/libpurgeablemem/test/purgeable_c_test.cpp b/libpurgeablemem/test/purgeable_c_test.cpp index 50611ccc8aeae75aadd28c6c7bd3d95f7b568366..d46d17762076b913c74417ed85133383708544a0 100644 --- a/libpurgeablemem/test/purgeable_c_test.cpp +++ b/libpurgeablemem/test/purgeable_c_test.cpp @@ -110,9 +110,7 @@ HWTEST_F(PurgeableCTest, ReadTest, TestSize.Level1) const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\0"; struct AlphabetInitParam initPara = {'A', 'Z'}; struct PurgMem *pobj = PurgMemCreate(27, InitAlphabet, &initPara); - std::thread reclaimThread(LoopReclaimPurgeable, UINT_MAX); - pthread_t reclaimPid = reclaimThread.native_handle(); - reclaimThread.detach(); + LoopReclaimPurgeable(1); unsigned int loopCount = 3; /* loop read content */ @@ -125,7 +123,6 @@ HWTEST_F(PurgeableCTest, ReadTest, TestSize.Level1) PurgMemEndRead(pobj); } - pthread_cancel(reclaimPid); /* destroy reclaimThread */ PurgMemDestroy(pobj); } @@ -134,9 +131,7 @@ HWTEST_F(PurgeableCTest, WriteTest, TestSize.Level1) const char alphabet[] = "CCCDEFGHIJKLMNOPQRSTUVWXYZ\0"; struct AlphabetInitParam initPara = {'A', 'Z'}; struct PurgMem *pobj = PurgMemCreate(27, InitAlphabet, &initPara); - std::thread reclaimThread(LoopReclaimPurgeable, UINT_MAX); - pthread_t reclaimPid = reclaimThread.native_handle(); - reclaimThread.detach(); + LoopReclaimPurgeable(1); struct AlphabetModifyParam a2b = {'A', 'B'}; struct AlphabetModifyParam b2c = {'B', 'C'}; @@ -150,7 +145,6 @@ HWTEST_F(PurgeableCTest, WriteTest, TestSize.Level1) std::cout << __func__ << ": ERROR! BeginRead failed." << std::endl; } - pthread_cancel(reclaimPid); /* destroy reclaimThread */ PurgMemDestroy(pobj); LoopReclaimPurgeable(3); } @@ -160,14 +154,8 @@ HWTEST_F(PurgeableCTest, ReadWriteTest, TestSize.Level1) const char alphabet[] = "DDDDEFGHIJKLMNOPQRSTUVWXYZ\0"; struct AlphabetInitParam initPara = {'A', 'Z'}; struct PurgMem *pobj = PurgMemCreate(27, InitAlphabet, &initPara); - /* loop reclaim thread */ - std::thread reclaimThread(LoopReclaimPurgeable, UINT_MAX); - pthread_t reclaimPid = reclaimThread.native_handle(); - reclaimThread.detach(); - /* loop read thread */ - std::thread readThread(LoopPrintAlphabet, pobj, UINT_MAX); - pthread_t readPid = readThread.native_handle(); - readThread.detach(); + LoopReclaimPurgeable(1); + LoopPrintAlphabet(pobj, 1); struct AlphabetModifyParam a2b = {'A', 'B'}; struct AlphabetModifyParam b2c = {'B', 'C'}; @@ -183,8 +171,6 @@ HWTEST_F(PurgeableCTest, ReadWriteTest, TestSize.Level1) std::cout << __func__ << ": ERROR! BeginRead failed." << std::endl; } - pthread_cancel(reclaimPid); /* destroy reclaimThread */ - pthread_cancel(readPid); /* destroy readThread */ PurgMemDestroy(pobj); }