sinojelly / mockcpp

Two C/C++ testing tools, mockcpp and testngpp.
Apache License 2.0
67 stars 42 forks source link

while running in linux multi-thread, maybe have coredump #41

Closed github-my closed 2 years ago

github-my commented 2 years ago

sample test code: `#include

include

include <sys/syscall.h>

include

include "gtest/gtest.h"

include "mockcpp/mokc.h"

int int_ret_func(int a, int b) { printf("call orginal function %s\n", FUNCTION); return a + b; }

int stub_int_ret_func(int a, int b) { int tid = (int)syscall(__NR_gettid); printf("call stub function %s, tid: %d\n", FUNCTION, tid); return a * b; }

void void_ret_func(int a, int b, int c) { printf("call orginal function %s\n", FUNCTION); }

void stub_void_ret_func(int a, int b, int c) { int tid = (int)syscall(__NR_gettid); printf("call stub function %s, tid: %d\n", FUNCTION, tid); }

void threadFunc(void arg) { while (1) { int ret = int_ret_func(2, 3); EXPECT_EQ(ret, 6); void_ret_func(2, 3, 4); usleep(1000); }

return NULL;

}

TEST(TestSuite, Factorial_Stub) { MOCKER(int_ret_func).stubs().will(invoke(stub_int_ret_func)); MOCKER(void_ret_func).stubs().will(invoke(stub_void_ret_func));

#define TID_CNT 8
pthread_t tid[TID_CNT];
for (int loop = 0; loop < TID_CNT; loop++) {
    pthread_create(&tid[loop], NULL, &threadFunc, NULL);
}

for (int loop = 0; loop < TID_CNT; loop++) {
    pthread_join(tid[loop], NULL);
}

}` running result: image

because of data using after free, need to fix

github-my commented 2 years ago

MR https://github.com/sinojelly/mockcpp/pull/42 fix it

github-my commented 2 years ago

test old testcase result:

[ RUN ] TestStubContainter::TestStubContainer::testShouldAlwaysReturnAStubBeforeItIsCompleted [ OK ] (9 us) [ RUN ] TestStubContainter::TestStubContainer::testShouldAlwaysReturnLastStubIfAllStubsHaveBeenReturnedEvenIfItHasBeenCompleted [ OK ] (9 us) [ RUN ] TestStubContainter::TestStubContainer::testShouldReturnNullIfNoStubWasAdded [ OK ] (3 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldBeAbleToConvertToPointerToInterfaceType [ OK ] (91 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldThrowExceptionIfTheNumberOfVptrExceedsTheMaxSettingOfConfiguration [ OK ] (60 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldBeAbleToSetMethod [ OK ] (67 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldThrowExceptionIfAMethodIsNotSet [ OK ] (87 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldThrowExceptionIfIndexOfVtblExceedsTheLimitationOfConfiguration [ OK ] (65 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldThrowExceptionIfIndexOfVtblExceedsTheNumberOfVptr [ OK ] (52 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldBeAbleToGetPreviouslySetIndexInvokableGetterByReturnedPointerToInterface [ OK ] (53 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldThrowExceptionIfItExpectedToBeDeletedButActuallyNotWhenItIsVerified [ OK ] (52 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldPassVerifyIfThePointerIsDeletedAsExpected [ OK ] (44 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldPassVerifyIfThePointerIsNotDeletedAsExpected [ OK ] (43 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldFailThrowExceptionWhileTryingToDeleteThePointerWhichWasExpectedNotTo [ OK ] (48 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldThrowExceptionIfAnObjectIsExpectedBothKeepAliveAndBeingDelete [ OK ] (58 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldThrowExceptionIfAnObjectIsExpectedBothKeepAliveAndBeingDelete2 [ OK ] (47 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldThrowExceptionWhenTryingToExpectsADeletedObjectKeepAlive [ OK ] (55 us) [ RUN ] TestVirtualTable::TestVirtualTable::shouldThrowAnExceptionWhenTryingToAccessMethodsOfADeleteObject [ OK ] (54 us)

===========================RESULT=========================== [ OK ] 274 cases from 32 suites ran successfully.

(0s 32116us) [root@8d528f027148 /sc/01.src/mockcpp]$

sinojelly commented 2 years ago

OK, thanks, merged.