1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
#include "gtest/gtest.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "nsBufferedStreams.h"
#include "nsStreamUtils.h"
#include "nsIThread.h"
#include "nsThreadUtils.h"
#include "Helpers.h"
// Helper function for creating a testing::AsyncStringStream
already_AddRefed<nsBufferedInputStream> CreateStream(uint32_t aSize,
nsCString& aBuffer) {
aBuffer.SetLength(aSize);
for (uint32_t i = 0; i < aSize; ++i) {
aBuffer.BeginWriting()[i] = i % 10;
}
nsCOMPtr<nsIInputStream> stream = new testing::AsyncStringStream(aBuffer);
RefPtr<nsBufferedInputStream> bis = new nsBufferedInputStream();
bis->Init(stream, aSize);
return bis.forget();
}
// Simple reading.
TEST(TestBufferedInputStream, SimpleRead)
{
const size_t kBufSize = 10;
nsCString buf;
RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
uint64_t length;
ASSERT_EQ(NS_OK, bis->Available(&length));
ASSERT_EQ((uint64_t)kBufSize, length);
char buf2[kBufSize];
uint32_t count;
ASSERT_EQ(NS_OK, bis->Read(buf2, sizeof(buf2), &count));
ASSERT_EQ(count, buf.Length());
ASSERT_TRUE(nsCString(buf.get(), kBufSize).Equals(nsCString(buf2, count)));
}
// Simple segment reading.
TEST(TestBufferedInputStream, SimpleReadSegments)
{
const size_t kBufSize = 10;
nsCString buf;
RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
char buf2[kBufSize];
uint32_t count;
ASSERT_EQ(NS_OK, bis->ReadSegments(NS_CopySegmentToBuffer, buf2, sizeof(buf2),
&count));
ASSERT_EQ(count, buf.Length());
ASSERT_TRUE(nsCString(buf.get(), kBufSize).Equals(nsCString(buf2, count)));
}
// AsyncWait - sync
TEST(TestBufferedInputStream, AsyncWait_sync)
{
const size_t kBufSize = 10;
nsCString buf;
RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
RefPtr<testing::InputStreamCallback> cb = new testing::InputStreamCallback();
ASSERT_EQ(NS_OK, bis->AsyncWait(cb, 0, 0, nullptr));
// Immediatelly called
ASSERT_TRUE(cb->Called());
}
// AsyncWait - async
TEST(TestBufferedInputStream, AsyncWait_async)
{
const size_t kBufSize = 10;
nsCString buf;
RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
RefPtr<testing::InputStreamCallback> cb = new testing::InputStreamCallback();
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
ASSERT_EQ(NS_OK, bis->AsyncWait(cb, 0, 0, thread));
ASSERT_FALSE(cb->Called());
// Eventually it is called.
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
}
// AsyncWait - sync - closureOnly
TEST(TestBufferedInputStream, AsyncWait_sync_closureOnly)
{
const size_t kBufSize = 10;
nsCString buf;
RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
RefPtr<testing::InputStreamCallback> cb = new testing::InputStreamCallback();
ASSERT_EQ(NS_OK, bis->AsyncWait(cb, nsIAsyncInputStream::WAIT_CLOSURE_ONLY, 0,
nullptr));
ASSERT_FALSE(cb->Called());
bis->CloseWithStatus(NS_ERROR_FAILURE);
// Immediatelly called
ASSERT_TRUE(cb->Called());
}
// AsyncWait - async
TEST(TestBufferedInputStream, AsyncWait_async_closureOnly)
{
const size_t kBufSize = 10;
nsCString buf;
RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
RefPtr<testing::InputStreamCallback> cb = new testing::InputStreamCallback();
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
ASSERT_EQ(NS_OK, bis->AsyncWait(cb, nsIAsyncInputStream::WAIT_CLOSURE_ONLY, 0,
thread));
ASSERT_FALSE(cb->Called());
bis->CloseWithStatus(NS_ERROR_FAILURE);
ASSERT_FALSE(cb->Called());
// Eventually it is called.
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
}
TEST(TestBufferedInputStream, AsyncWait_after_close)
{
const size_t kBufSize = 10;
nsCString buf;
RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
nsCOMPtr<nsIThread> eventTarget = do_GetCurrentThread();
auto cb = mozilla::MakeRefPtr<testing::InputStreamCallback>();
ASSERT_EQ(NS_OK, bis->AsyncWait(cb, 0, 0, eventTarget));
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
ASSERT_EQ(NS_OK, bis->Close());
cb = mozilla::MakeRefPtr<testing::InputStreamCallback>();
ASSERT_EQ(NS_OK, bis->AsyncWait(cb, 0, 0, eventTarget));
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
}
TEST(TestBufferedInputStream, AsyncLengthWait_after_close)
{
nsCString buf{"The Quick Brown Fox Jumps over the Lazy Dog"};
const size_t kBufSize = 44;
RefPtr<nsBufferedInputStream> bis = CreateStream(kBufSize, buf);
nsCOMPtr<nsIThread> eventTarget = do_GetCurrentThread();
auto cb = mozilla::MakeRefPtr<testing::LengthCallback>();
ASSERT_EQ(NS_OK, bis->AsyncLengthWait(cb, eventTarget));
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
uint64_t length;
ASSERT_EQ(NS_OK, bis->Available(&length));
ASSERT_EQ((uint64_t)kBufSize, length);
cb = mozilla::MakeRefPtr<testing::LengthCallback>();
ASSERT_EQ(NS_OK, bis->AsyncLengthWait(cb, eventTarget));
MOZ_ALWAYS_TRUE(mozilla::SpinEventLoopUntil([&]() { return cb->Called(); }));
ASSERT_TRUE(cb->Called());
}
|