| 1 |
/* |
| 2 |
* barrier -- mouse and keyboard sharing utility |
| 3 |
* Copyright (C) 2015-2016 Symless Ltd. |
| 4 |
* |
| 5 |
* This package is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* found in the file LICENSE that should have accompanied this file. |
| 8 |
* |
| 9 |
* This package is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#define BARRIER_TEST_ENV |
| 19 |
|
| 20 |
#include "test/mock/ipc/MockIpcServer.h" |
| 21 |
|
| 22 |
#include "mt/Thread.h" |
| 23 |
#include "ipc/IpcLogOutputter.h" |
| 24 |
#include "base/String.h" |
| 25 |
#include "common/common.h" |
| 26 |
|
| 27 |
#include "test/global/gmock.h" |
| 28 |
#include "test/global/gtest.h" |
| 29 |
|
| 30 |
// HACK: ipc logging only used on windows anyway |
| 31 |
#if WINAPI_MSWINDOWS |
| 32 |
|
| 33 |
using ::testing::_; |
| 34 |
using ::testing::Return; |
| 35 |
using ::testing::Matcher; |
| 36 |
using ::testing::MatcherCast; |
| 37 |
using ::testing::Property; |
| 38 |
using ::testing::StrEq; |
| 39 |
using ::testing::AtLeast; |
| 40 |
|
| 41 |
using namespace barrier; |
| 42 |
|
| 43 |
inline const Matcher<const IpcMessage&> IpcLogLineMessageEq(const String& s) { |
| 44 |
const Matcher<const IpcLogLineMessage&> m( |
| 45 |
Property(&IpcLogLineMessage::logLine, StrEq(s))); |
| 46 |
return MatcherCast<const IpcMessage&>(m); |
| 47 |
} |
| 48 |
|
| 49 |
TEST(IpcLogOutputterTests, write_threadingEnabled_bufferIsSent) |
| 50 |
{ |
| 51 |
MockIpcServer mockServer; |
| 52 |
mockServer.delegateToFake(); |
| 53 |
|
| 54 |
ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); |
| 55 |
|
| 56 |
EXPECT_CALL(mockServer, hasClients(_)).Times(AtLeast(3)); |
| 57 |
EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 1\n"), _)).Times(1); |
| 58 |
EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 2\n"), _)).Times(1); |
| 59 |
|
| 60 |
IpcLogOutputter outputter(mockServer, kIpcClientUnknown, true); |
| 61 |
outputter.write(kNOTE, "mock 1"); |
| 62 |
mockServer.waitForSend(); |
| 63 |
outputter.write(kNOTE, "mock 2"); |
| 64 |
mockServer.waitForSend(); |
| 65 |
} |
| 66 |
|
| 67 |
TEST(IpcLogOutputterTests, write_overBufferMaxSize_firstLineTruncated) |
| 68 |
{ |
| 69 |
MockIpcServer mockServer; |
| 70 |
|
| 71 |
ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); |
| 72 |
EXPECT_CALL(mockServer, hasClients(_)).Times(1); |
| 73 |
EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 2\nmock 3\n"), _)).Times(1); |
| 74 |
|
| 75 |
IpcLogOutputter outputter(mockServer, kIpcClientUnknown, false); |
| 76 |
outputter.bufferMaxSize(2); |
| 77 |
|
| 78 |
// log more lines than the buffer can contain |
| 79 |
outputter.write(kNOTE, "mock 1"); |
| 80 |
outputter.write(kNOTE, "mock 2"); |
| 81 |
outputter.write(kNOTE, "mock 3"); |
| 82 |
outputter.sendBuffer(); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(IpcLogOutputterTests, write_underBufferMaxSize_allLinesAreSent) |
| 86 |
{ |
| 87 |
MockIpcServer mockServer; |
| 88 |
|
| 89 |
ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); |
| 90 |
|
| 91 |
EXPECT_CALL(mockServer, hasClients(_)).Times(1); |
| 92 |
EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 1\nmock 2\n"), _)).Times(1); |
| 93 |
|
| 94 |
IpcLogOutputter outputter(mockServer, kIpcClientUnknown, false); |
| 95 |
outputter.bufferMaxSize(2); |
| 96 |
|
| 97 |
// log more lines than the buffer can contain |
| 98 |
outputter.write(kNOTE, "mock 1"); |
| 99 |
outputter.write(kNOTE, "mock 2"); |
| 100 |
outputter.sendBuffer(); |
| 101 |
} |
| 102 |
|
| 103 |
// HACK: temporarily disable this intermittently failing unit test. |
| 104 |
// when the build machine is under heavy load, a race condition |
| 105 |
// usually happens. |
| 106 |
#if 0 |
| 107 |
TEST(IpcLogOutputterTests, write_overBufferRateLimit_lastLineTruncated) |
| 108 |
{ |
| 109 |
MockIpcServer mockServer; |
| 110 |
|
| 111 |
ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); |
| 112 |
|
| 113 |
EXPECT_CALL(mockServer, hasClients(_)).Times(2); |
| 114 |
EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 1\nmock 2\n"), _)).Times(1); |
| 115 |
EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 4\nmock 5\n"), _)).Times(1); |
| 116 |
|
| 117 |
IpcLogOutputter outputter(mockServer, false); |
| 118 |
outputter.bufferRateLimit(2, 1); // 1s |
| 119 |
|
| 120 |
// log 1 more line than the buffer can accept in time limit. |
| 121 |
outputter.write(kNOTE, "mock 1"); |
| 122 |
outputter.write(kNOTE, "mock 2"); |
| 123 |
outputter.write(kNOTE, "mock 3"); |
| 124 |
|
| 125 |
outputter.sendBuffer(); |
| 126 |
|
| 127 |
// after waiting the time limit send another to make sure |
| 128 |
// we can log after the time limit passes. |
| 129 |
// HACK: sleep causes the unit test to fail intermittently, |
| 130 |
// so lets try 100ms (there must be a better way to solve this) |
| 131 |
ARCH->sleep(2); // 2s |
| 132 |
outputter.write(kNOTE, "mock 4"); |
| 133 |
outputter.write(kNOTE, "mock 5"); |
| 134 |
outputter.write(kNOTE, "mock 6"); |
| 135 |
|
| 136 |
outputter.sendBuffer(); |
| 137 |
} |
| 138 |
#endif |
| 139 |
|
| 140 |
TEST(IpcLogOutputterTests, write_underBufferRateLimit_allLinesAreSent) |
| 141 |
{ |
| 142 |
MockIpcServer mockServer; |
| 143 |
|
| 144 |
ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); |
| 145 |
|
| 146 |
EXPECT_CALL(mockServer, hasClients(_)).Times(2); |
| 147 |
EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 1\nmock 2\n"), _)).Times(1); |
| 148 |
EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 3\nmock 4\n"), _)).Times(1); |
| 149 |
|
| 150 |
IpcLogOutputter outputter(mockServer, kIpcClientUnknown, false); |
| 151 |
outputter.bufferRateLimit(4, 1); // 1s (should be plenty of time) |
| 152 |
|
| 153 |
// log 1 more line than the buffer can accept in time limit. |
| 154 |
outputter.write(kNOTE, "mock 1"); |
| 155 |
outputter.write(kNOTE, "mock 2"); |
| 156 |
outputter.sendBuffer(); |
| 157 |
|
| 158 |
// after waiting the time limit send another to make sure |
| 159 |
// we can log after the time limit passes. |
| 160 |
outputter.write(kNOTE, "mock 3"); |
| 161 |
outputter.write(kNOTE, "mock 4"); |
| 162 |
outputter.sendBuffer(); |
| 163 |
} |
| 164 |
|
| 165 |
#endif // WINAPI_MSWINDOWS |