TEST_F(SignalTest, DeferredSynchronousSignal) { cout << "Instantiating signal object" << endl; Signal<int, int> testSignal; BasicTimer bt; cout << "Connecting signal" << endl; bt.start(); testSignal.connectSlot(ExecutorScheme::DEFERRED_SYNCHRONOUS, staticSumFunction); bt.stop(); cout << "Time to connect: " << bt.getElapsedMilliseconds() << "ms" << endl; cout << "Emitting signal" << endl; bt.start(); testSignal(1, 2); bt.stop(); cout << "Time to emit: " << bt.getElapsedMilliseconds() << "ms" << endl; ASSERT_NE(globalStaticIntX, 3); testSignal.invokeDeferred(); ASSERT_EQ(globalStaticIntX, 3); testSignal.disconnectSlot(0); }
TEST_F(SignalTest, MultiEmit){ cout << "Instantiating signal object" << endl; Signal<int, int> testSignal; BasicTimer bt; BasicTimer bt2; cout << "Connecting signal" << endl; bt.start(); int id = testSignal.connectSlot(ExecutorScheme::STRAND, staticSumFunction); bt.stop(); cout << "Time to connect: " << bt.getElapsedMilliseconds() << "ms" << endl; cout << "Emitting signal" << endl; bt.start(); list<thread> threads; const uint32_t nThreads = 64; std::atomic<uint32_t> threadsRemaining{nThreads}; for (uint32_t i=0; i<nThreads; i++){ threads.emplace_back([&, i](){ for (uint32_t i=0; i<1000000; ++i){ testSignal.emitSignal(1, 2); } uint32_t tr = threadsRemaining.fetch_sub(1); cout << "Threads remaining: " << tr-1 << endl; cout << "globalStaticInt: " << globalStaticIntX << endl; }); } bt.stop(); bt2.start(); while (bt2.getElapsedSeconds() < 60.0) { if (globalStaticIntX == 1000000*3*nThreads) { bt2.stop(); break; } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } //ASSERT_LT(bt2.getElapsedSeconds(), 0.1); cout << "Time to emit: " << bt.getElapsedMilliseconds() << "ms" << endl; cout << "Time to emit and process: " << bt2.getElapsedMilliseconds() << "ms" << endl; cout << "Net time to process: " << bt2.getElapsedMilliseconds() - bt.getElapsedMilliseconds() << "ms" << endl; ASSERT_EQ(0, id); testSignal.disconnectAllSlots(); for (auto &t : threads) t.join(); }
TEST_F(SignalTest, StrandSignal) { cout << "Instantiating signal object" << endl; Signal<int, int> testSignal; BasicTimer bt; BasicTimer bt2; cout << "Connecting signal" << endl; bt.start(); int id = testSignal.connectSlot(ExecutorScheme::STRAND, staticSumFunction); bt.stop(); cout << "Time to connect: " << bt.getElapsedMilliseconds() << "ms" << endl; cout << "Emitting signal" << endl; bt.start(); bt2.start(); testSignal.emitSignal(1, 2); bt.stop(); while (bt2.getElapsedSeconds() < 0.1) { if (globalStaticIntX == 3) { bt2.stop(); break; } } ASSERT_LT(bt2.getElapsedSeconds(), 0.1); ASSERT_EQ(globalStaticIntX, 3); cout << "Time to emit: " << bt.getElapsedMilliseconds() << "ms" << endl; cout << "Time to emit and process: " << bt2.getElapsedMilliseconds() << "ms" << endl; cout << "Net time to process: " << bt2.getElapsedMilliseconds() - bt.getElapsedMilliseconds() << "ms" << endl; ASSERT_EQ(0, id); testSignal.disconnectAllSlots(); }
TEST_F(SignalTest, MultipleConnectionTypes) { cout << "Testing multiple connection types for single signal" << endl; Signal<BigThing> testSignal; cout << "Connecting signals" << endl; list<int> ids; ids.push_back(testSignal.connectSlot(ExecutorScheme::SYNCHRONOUS, pushToQueue)); ids.push_back(testSignal.connectSlot(ExecutorScheme::ASYNCHRONOUS, pushToQueue)); ids.push_back(testSignal.connectSlot(ExecutorScheme::STRAND, pushToQueue)); cout << "Emitting signal" << endl; BasicTimer bt; BasicTimer bt2; bt.start(); bt2.start(); BigThing bigthing; for (uint32_t i = 0; i < 10; i++) { testSignal.emitSignal(bigthing); } bt2.stop(); cout << "Dequeueing" << endl; for (int i = 0; i < 30; i++) { sq.dequeue(); } bt.stop(); cout << "Signals emitted" << endl; cout << "Total time for emission: " << bt2.getElapsedMilliseconds() << "ms" << endl; cout << "Total time for emission and processing: " << bt.getElapsedMilliseconds() << "ms" << endl; cout << "Average emission+processing time: " << bt.getElapsedMilliseconds() / 10.0 << "ms" << endl; ASSERT_LT(bt2.getElapsedSeconds(), 0.1); ASSERT_LT(bt.getElapsedSeconds(), 0.2); }