PIThreadNotifier, rewrite PIObject::deleteLater()

tests for PIThreadNotifier and PIObject::deleteLater()
This commit is contained in:
Andrey
2021-10-29 18:20:48 +03:00
parent 6e5a5a6ade
commit 48c885e12a
9 changed files with 196 additions and 75 deletions

View File

@@ -198,6 +198,6 @@ TEST_F(ConditionVariable, waitFor_is_unblock_when_condition_and_notifyOne) {
condition = true;
m.unlock();
variable->notifyOne();
msleep(WAIT_THREAD_TIME_MS);
piMSleep(WAIT_THREAD_TIME_MS);
ASSERT_FALSE(thread->isRunning());
}

View File

@@ -0,0 +1,34 @@
#include "gtest/gtest.h"
#include "pithreadnotifier.h"
TEST(PIThreadNotifierTest, One) {
PIThreadNotifier n;
int cnt = 0;
PIThread t1([&n, &cnt](){n.wait(); cnt++;}, true);
piMSleep(10);
n.notifyOnce();
piMSleep(10);
ASSERT_EQ(cnt, 1);
n.notifyOnce();
piMSleep(10);
ASSERT_EQ(cnt, 2);
}
TEST(PIThreadNotifierTest, Two) {
PIThreadNotifier n;
int cnt1 = 0;
int cnt2 = 0;
int cnt3 = 0;
PIThread t1([&n, &cnt1](){n.wait(); cnt1++; piMSleep(2);}, true);
PIThread t2([&n, &cnt2](){n.wait(); cnt2++; piMSleep(2);}, true);
PIThread t3([&n, &cnt3](){n.notifyOnce(); cnt3++; piMSleep(1);}, true);
piMSleep(20);
t3.stop(true);
piMSleep(100);
t1.stop();
t2.stop();
ASSERT_EQ(cnt1+cnt2, cnt3);
}

View File

@@ -0,0 +1,51 @@
#include "gtest/gtest.h"
#include "piobject.h"
std::atomic<int> obj_cnt;
class Send: public PIObject {
PIOBJECT(Send)
public:
Send() {obj_cnt++;}
~Send() {obj_cnt--;}
EVENT1(ev, PIObject * , o)
};
class Recv: public PIObject {
PIOBJECT(Recv)
public:
Recv() {obj_cnt++;}
~Recv() {obj_cnt--;}
EVENT_HANDLER1(void, eh, PIObject * , o) {
o->deleteLater();
piMSleep(10);
}
};
TEST(Piobject, deleteLater) {
obj_cnt = 0;
Send * s = new Send();
Recv * r = new Recv();
CONNECTU(s, ev, r, eh);
s->ev(r);
r->deleteLater();
s->deleteLater();
piMSleep(100);
ASSERT_EQ(obj_cnt, 0);
PIVector<Send *> s2;
s2.resize(100, new Send());
for (auto o : s2) o->deleteLater();
piMSleep(10);
ASSERT_EQ(obj_cnt, 0);
s2.clear();
PIVector<Recv *> r2;
r2.resize(100, [](size_t i){return new Recv();});
for (auto o : r2) o->deleteLater();
piMSleep(10);
ASSERT_EQ(obj_cnt, 0);
r2.clear();
}