Skip to content

Commit aa81a0b

Browse files
committed
CDRIVER-756: Basic stream test
1 parent a38f0c7 commit aa81a0b

File tree

1 file changed

+99
-2
lines changed

1 file changed

+99
-2
lines changed

tests/test-mongoc-stream.c

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <fcntl.h>
44
#include <mongoc.h>
5+
#include <mongoc-stream-private.h>
56
#include <stdlib.h>
67

78
#include "TestSuite.h"
@@ -26,7 +27,13 @@ test_buffered_basic (void)
2627
iov.iov_len = sizeof buf;
2728
iov.iov_base = buf;
2829
r = mongoc_stream_readv(buffered, &iov, 1, iov.iov_len, -1);
29-
BSON_ASSERT(r == iov.iov_len);
30+
if (r != iov.iov_len) {
31+
char msg[100];
32+
33+
bson_snprintf(msg, 100, "Expected %lld got %llu",
34+
(long long)r, (unsigned long long)iov.iov_len);
35+
ASSERT_CMPSTR(msg, "failed");
36+
}
3037

3138
/* cleanup */
3239
mongoc_stream_destroy(buffered);
@@ -52,16 +59,106 @@ test_buffered_oversized (void)
5259
iov.iov_len = sizeof buf;
5360
iov.iov_base = buf;
5461
r = mongoc_stream_readv(buffered, &iov, 1, iov.iov_len, -1);
55-
BSON_ASSERT(r == iov.iov_len);
62+
if (r != iov.iov_len) {
63+
char msg[100];
64+
65+
bson_snprintf(msg, 100, "Expected %lld got %llu",
66+
(long long)r, (unsigned long long)iov.iov_len);
67+
ASSERT_CMPSTR(msg, "failed");
68+
}
5669

5770
/* cleanup */
5871
mongoc_stream_destroy(buffered);
5972
}
6073

6174

75+
typedef struct
76+
{
77+
mongoc_stream_t vtable;
78+
ssize_t rval;
79+
} failing_stream_t;
80+
81+
static ssize_t
82+
failing_stream_writev (mongoc_stream_t *stream,
83+
mongoc_iovec_t *iov,
84+
size_t iovcnt,
85+
int32_t timeout_msec)
86+
{
87+
failing_stream_t *fstream = (failing_stream_t *)stream;
88+
89+
return fstream->rval;
90+
}
91+
92+
void
93+
failing_stream_destroy (mongoc_stream_t *stream)
94+
{
95+
bson_free (stream);
96+
}
97+
98+
static mongoc_stream_t *
99+
failing_stream_new (ssize_t rval)
100+
{
101+
failing_stream_t *stream;
102+
103+
stream = bson_malloc0 (sizeof *stream);
104+
stream->vtable.type = 999;
105+
stream->vtable.writev = failing_stream_writev;
106+
stream->vtable.destroy = failing_stream_destroy;
107+
stream->rval = rval;
108+
109+
return (mongoc_stream_t *)stream;
110+
}
111+
112+
113+
static void
114+
test_stream_writev_full (void)
115+
{
116+
mongoc_stream_t *error_stream = failing_stream_new (-1);
117+
mongoc_stream_t *short_stream = failing_stream_new (10);
118+
mongoc_stream_t *success_stream = failing_stream_new (100);
119+
char bufa[20];
120+
char bufb[80];
121+
bool r;
122+
mongoc_iovec_t iov[2];
123+
bson_error_t error = { 0 };
124+
const char *error_message = "Failure during socket delivery: ";
125+
const char *short_message =
126+
"Failure to send all requested bytes (only sent: 10/100 in 100ms) during socket delivery";
127+
128+
iov[0].iov_base = bufa;
129+
iov[0].iov_len = sizeof (bufa);
130+
iov[1].iov_base = bufb;
131+
iov[1].iov_len = sizeof (bufb);
132+
133+
errno = EINVAL;
134+
r = _mongoc_stream_writev_full (error_stream, iov, 2, 100, &error);
135+
136+
BSON_ASSERT (!r);
137+
ASSERT_CMPINT (error.domain, ==, MONGOC_ERROR_STREAM);
138+
ASSERT_CMPINT (error.code, ==, MONGOC_ERROR_STREAM_SOCKET);
139+
ASSERT_STARTSWITH (error.message, error_message);
140+
141+
errno = 0;
142+
r = _mongoc_stream_writev_full (short_stream, iov, 2, 100, &error);
143+
BSON_ASSERT (!r);
144+
ASSERT_CMPINT (error.domain, ==, MONGOC_ERROR_STREAM);
145+
ASSERT_CMPINT (error.code, ==, MONGOC_ERROR_STREAM_SOCKET);
146+
ASSERT_CMPSTR (error.message, short_message);
147+
148+
errno = 0;
149+
r = _mongoc_stream_writev_full (success_stream, iov, 2, 100, &error);
150+
BSON_ASSERT (r);
151+
152+
mongoc_stream_destroy (error_stream);
153+
mongoc_stream_destroy (short_stream);
154+
mongoc_stream_destroy (success_stream);
155+
}
156+
157+
62158
void
63159
test_stream_install (TestSuite *suite)
64160
{
65161
TestSuite_Add (suite, "/Stream/buffered/basic", test_buffered_basic);
66162
TestSuite_Add (suite, "/Stream/buffered/oversized", test_buffered_oversized);
163+
TestSuite_Add (suite, "/Stream/writev_full", test_stream_writev_full);
67164
}

0 commit comments

Comments
 (0)