2
2
3
3
#include <fcntl.h>
4
4
#include <mongoc.h>
5
+ #include <mongoc-stream-private.h>
5
6
#include <stdlib.h>
6
7
7
8
#include "TestSuite.h"
@@ -26,7 +27,13 @@ test_buffered_basic (void)
26
27
iov .iov_len = sizeof buf ;
27
28
iov .iov_base = buf ;
28
29
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
+ }
30
37
31
38
/* cleanup */
32
39
mongoc_stream_destroy (buffered );
@@ -52,16 +59,106 @@ test_buffered_oversized (void)
52
59
iov .iov_len = sizeof buf ;
53
60
iov .iov_base = buf ;
54
61
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
+ }
56
69
57
70
/* cleanup */
58
71
mongoc_stream_destroy (buffered );
59
72
}
60
73
61
74
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
+
62
158
void
63
159
test_stream_install (TestSuite * suite )
64
160
{
65
161
TestSuite_Add (suite , "/Stream/buffered/basic" , test_buffered_basic );
66
162
TestSuite_Add (suite , "/Stream/buffered/oversized" , test_buffered_oversized );
163
+ TestSuite_Add (suite , "/Stream/writev_full" , test_stream_writev_full );
67
164
}
0 commit comments