1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "../SyncQueue.h"
18 
19 #include <gtest/gtest.h>
20 #include <thread>
21 
22 namespace android {
23 
24 // --- SyncQueueTest ---
25 
26 // Validate basic pop and push operation.
TEST(SyncQueueTest,AddAndRemove)27 TEST(SyncQueueTest, AddAndRemove) {
28     SyncQueue<int> queue;
29 
30     queue.push(1);
31     ASSERT_EQ(queue.pop(), 1);
32 
33     queue.push(3);
34     ASSERT_EQ(queue.pop(), 3);
35 
36     ASSERT_EQ(std::nullopt, queue.pop());
37 }
38 
39 // Make sure the queue maintains FIFO order.
40 // Add elements and remove them, and check the order.
TEST(SyncQueueTest,isFIFO)41 TEST(SyncQueueTest, isFIFO) {
42     SyncQueue<int> queue;
43 
44     constexpr int numItems = 10;
45     for (int i = 0; i < numItems; i++) {
46         queue.push(static_cast<int>(i));
47     }
48     for (int i = 0; i < numItems; i++) {
49         ASSERT_EQ(queue.pop(), static_cast<int>(i));
50     }
51 }
52 
53 // Make sure the queue has strict capacity limits.
TEST(SyncQueueTest,QueueReachesCapacity)54 TEST(SyncQueueTest, QueueReachesCapacity) {
55     constexpr size_t capacity = 3;
56     SyncQueue<int> queue(capacity);
57 
58     // First 3 elements should be added successfully
59     ASSERT_TRUE(queue.push(1));
60     ASSERT_TRUE(queue.push(2));
61     ASSERT_TRUE(queue.push(3));
62     ASSERT_FALSE(queue.push(4)) << "Queue should reach capacity at size " << capacity;
63 }
64 
TEST(SyncQueueTest,AllowsMultipleThreads)65 TEST(SyncQueueTest, AllowsMultipleThreads) {
66     SyncQueue<int> queue;
67 
68     // Test with a large number of items to increase likelihood that threads overlap
69     constexpr int numItems = 100;
70 
71     // Fill queue from a different thread
72     std::thread fillQueue([&queue]() {
73         for (int i = 0; i < numItems; i++) {
74             queue.push(static_cast<int>(i));
75         }
76     });
77 
78     // Make sure all elements are received in correct order
79     for (int i = 0; i < numItems; i++) {
80         // Since popping races with the thread that's filling the queue,
81         // keep popping until we get something back
82         std::optional<int> popped;
83         do {
84             popped = queue.pop();
85         } while (!popped);
86         ASSERT_EQ(popped, static_cast<int>(i));
87     }
88 
89     fillQueue.join();
90 }
91 
92 } // namespace android
93