1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "logd/LogEventQueue.h"
16 
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <stdio.h>
20 
21 #include <thread>
22 
23 #include "stats_event.h"
24 #include "tests/statsd_test_util.h"
25 
26 namespace android {
27 namespace os {
28 namespace statsd {
29 
30 using namespace android;
31 using namespace testing;
32 
33 using std::unique_ptr;
34 
35 namespace {
36 
makeLogEvent(uint64_t timestampNs)37 std::unique_ptr<LogEvent> makeLogEvent(uint64_t timestampNs) {
38     AStatsEvent* statsEvent = AStatsEvent_obtain();
39     AStatsEvent_setAtomId(statsEvent, 10);
40     AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
41 
42     std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
43     parseStatsEventToLogEvent(statsEvent, logEvent.get());
44     return logEvent;
45 }
46 
47 } // anonymous namespace
48 
49 #ifdef __ANDROID__
TEST(LogEventQueue_test,TestGoodConsumer)50 TEST(LogEventQueue_test, TestGoodConsumer) {
51     LogEventQueue queue(50);
52     int64_t timeBaseNs = 100;
53     std::thread writer([&queue, timeBaseNs] {
54         for (int i = 0; i < 100; i++) {
55             int64_t oldestEventNs;
56             bool success = queue.push(makeLogEvent(timeBaseNs + i * 1000), &oldestEventNs);
57             EXPECT_TRUE(success);
58             std::this_thread::sleep_for(std::chrono::milliseconds(1));
59         }
60     });
61 
62     std::thread reader([&queue, timeBaseNs] {
63         for (int i = 0; i < 100; i++) {
64             auto event = queue.waitPop();
65             EXPECT_TRUE(event != nullptr);
66             // All events are in right order.
67             EXPECT_EQ(timeBaseNs + i * 1000, event->GetElapsedTimestampNs());
68         }
69     });
70 
71     reader.join();
72     writer.join();
73 }
74 
TEST(LogEventQueue_test,TestSlowConsumer)75 TEST(LogEventQueue_test, TestSlowConsumer) {
76     LogEventQueue queue(50);
77     int64_t timeBaseNs = 100;
78     std::thread writer([&queue, timeBaseNs] {
79         int failure_count = 0;
80         int64_t oldestEventNs;
81         for (int i = 0; i < 100; i++) {
82             bool success = queue.push(makeLogEvent(timeBaseNs + i * 1000), &oldestEventNs);
83             if (!success) failure_count++;
84             std::this_thread::sleep_for(std::chrono::milliseconds(1));
85         }
86 
87         // There is some remote chance that reader thread not get chance to run before writer thread
88         // ends. That's why the following comparison is not "==".
89         // There will be at least 45 events lost due to overflow.
90         EXPECT_TRUE(failure_count >= 45);
91         // The oldest event must be at least the 6th event.
92         EXPECT_TRUE(oldestEventNs <= (100 + 5 * 1000));
93     });
94 
95     std::thread reader([&queue, timeBaseNs] {
96         // The consumer quickly processed 5 events, then it got stuck (not reading anymore).
97         for (int i = 0; i < 5; i++) {
98             auto event = queue.waitPop();
99             EXPECT_TRUE(event != nullptr);
100             // All events are in right order.
101             EXPECT_EQ(timeBaseNs + i * 1000, event->GetElapsedTimestampNs());
102         }
103     });
104 
105     reader.join();
106     writer.join();
107 }
108 
109 #else
110 GTEST_LOG_(INFO) << "This test does nothing.\n";
111 #endif
112 
113 }  // namespace statsd
114 }  // namespace os
115 }  // namespace android
116