@@ -34,6 +34,8 @@ limitations under the License.
3434#include < thread>
3535#include < vector>
3636#include < photon/common/alog.h>
37+ #include < gtest/gtest.h>
38+ #include < gflags/gflags.h>
3739#include " ../../test/ci-tools.h"
3840
3941static constexpr size_t sender_num = 4 ;
@@ -86,7 +88,7 @@ int test_queue(const char *name, QType &queue) {
8688 auto begin = std::chrono::steady_clock::now ();
8789 for (size_t i = 0 ; i < receiver_num; i++) {
8890 receivers.emplace_back ([i, &queue] {
89- photon::set_cpu_affinity (i);
91+ photon::set_cpu_affinity (i * 2 );
9092 std::chrono::nanoseconds rspent (std::chrono::nanoseconds (0 ));
9193 for (size_t x = 0 ; x < items_num / receiver_num; x++) {
9294 int t;
@@ -109,7 +111,7 @@ int test_queue(const char *name, QType &queue) {
109111 }
110112 for (size_t i = 0 ; i < sender_num; i++) {
111113 senders.emplace_back ([i, &queue] {
112- photon::set_cpu_affinity (i);
114+ photon::set_cpu_affinity (i * 2 + 1 );
113115 std::chrono::nanoseconds wspent{std::chrono::nanoseconds (0 )};
114116 for (size_t x = 0 ; x < items_num / sender_num; x++) {
115117 auto tm = std::chrono::high_resolution_clock::now ();
@@ -135,9 +137,8 @@ int test_queue(const char *name, QType &queue) {
135137 LOG_DEBUG (" ` ` p ` c, ` items, Spent ` us" , name, sender_num, receiver_num, items_num,
136138 std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count ());
137139 for (size_t i = 0 ; i < items_num / sender_num; i++) {
138- if (sc[i] != rc[i] || sc[i] != sender_num) {
139- LOG_DEBUG (" MISMATCH ` ` `" , i, sc[i].load (), rc[i].load ());
140- }
140+ EXPECT_EQ (sc[i], rc[i]);
141+ EXPECT_EQ (sc[i].load (), (int )sender_num);
141142 sc[i] = 0 ;
142143 rc[i] = 0 ;
143144 }
@@ -153,7 +154,7 @@ int test_queue_batch(const char *name, QType &queue) {
153154 auto begin = std::chrono::steady_clock::now ();
154155 for (size_t i = 0 ; i < receiver_num; i++) {
155156 receivers.emplace_back ([i, &queue] {
156- photon::set_cpu_affinity (i);
157+ photon::set_cpu_affinity (i * 2 );
157158 int buffer[32 ];
158159 size_t size;
159160 int amount = items_num / receiver_num;
@@ -181,7 +182,7 @@ int test_queue_batch(const char *name, QType &queue) {
181182 }
182183 for (size_t i = 0 ; i < sender_num; i++) {
183184 senders.emplace_back ([i, &queue] {
184- photon::set_cpu_affinity (i);
185+ photon::set_cpu_affinity (i * 2 + 1 );
185186 std::vector<int > vec;
186187 vec.resize (items_num / sender_num);
187188 for (size_t x = 0 ; x < items_num / sender_num; x++) {
@@ -215,26 +216,33 @@ int test_queue_batch(const char *name, QType &queue) {
215216 LOG_DEBUG (" ` ` p ` c, ` items, Spent ` us" , name, sender_num, receiver_num, items_num,
216217 std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count ());
217218 for (size_t i = 0 ; i < items_num / sender_num; i++) {
218- if (sc[i] != rc[i] || sc[i] != sender_num) {
219- LOG_DEBUG (" MISMATCH ` ` `" , i, sc[i].load (), rc[i].load ());
220- }
219+ EXPECT_EQ (sc[i], rc[i]);
220+ EXPECT_EQ (sc[i].load (), (int )sender_num);
221221 sc[i] = 0 ;
222222 rc[i] = 0 ;
223223 }
224224 std::this_thread::sleep_for (std::chrono::seconds (1 ));
225225 return 0 ;
226226}
227227
228- int main ( ) {
228+ TEST (lockfree_queue, test_queue ) {
229229#ifdef TESTING_ENABLE_BOOST
230230 test_queue<NoLock, NoLock>(" BoostQueue" , bqueue);
231231#endif
232232 test_queue<NoLock, NoLock>(" PhotonLockfreeMPMCQueue" , lqueue);
233233 test_queue<NoLock, NoLock>(" PhotonLockfreeBatchMPMCQueue" , lbqueue);
234- test_queue_batch<NoLock, NoLock>(" PhotonLockfreeBatchMPMCQueue+Batch" , lbqueue);
234+ test_queue_batch<NoLock, NoLock>(" PhotonLockfreeBatchMPMCQueue+Batch" ,
235+ lbqueue);
235236#ifdef TESTING_ENABLE_BOOST
236237 test_queue<WithLock, WithLock>(" BoostSPSCQueue" , squeue);
237238#endif
238239 test_queue<WithLock, WithLock>(" PhotonSPSCQueue" , cqueue);
239240 test_queue_batch<WithLock, WithLock>(" PhotonSPSCQueue+Batch" , cqueue);
240241}
242+
243+ int main (int argc, char **arg) {
244+ if (!photon::is_using_default_engine ()) return 0 ;
245+ ::testing::InitGoogleTest (&argc, arg);
246+ gflags::ParseCommandLineFlags (&argc, &arg, true );
247+ return RUN_ALL_TESTS ();
248+ }
0 commit comments