Skip to content

Commit 827d2d8

Browse files
committed
Merge pull request #270 from jmikola/cdriver-851
CDRIVER-851: Negative wtimeout values are invalid
2 parents 2c9f0bf + 934dd33 commit 827d2d8

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

doc/mongoc_write_concern_set_wmajority.page

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ mongoc_write_concern_set_wmajority (mongoc_write_concern_t *write_concern,
2121
<title>Parameters</title>
2222
<table>
2323
<tr><td><p>write_concern</p></td><td><p>A <code xref="mongoc_write_concern_t">mongoc_write_concern_t</code>.</p></td></tr>
24-
<tr><td><p>wtimeout_msec</p></td><td><p>A int32_t.</p></td></tr>
24+
<tr><td><p>wtimeout_msec</p></td><td><p>A positive <code>int32_t</code> or zero.</p></td></tr>
2525
</table>
2626
</section>
2727

2828
<section id="description">
2929
<title>Description</title>
3030
<p>Sets if the write must have been propagated to a majority of nodes before indicating write success.</p>
31+
<p>The timeout specifies how long, in milliseconds, the server should wait before indicating that the write has failed. This is not the same as a socket timeout. A value of zero may be used to indicate no timeout.</p>
3132
</section>
3233

3334
</page>

doc/mongoc_write_concern_set_wtimeout.page

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ mongoc_write_concern_set_wtimeout (mongoc_write_concern_t *write_concern,
2222
<title>Parameters</title>
2323
<table>
2424
<tr><td><p>write_concern</p></td><td><p>A <code xref="mongoc_write_concern_t">mongoc_write_concern_t</code>.</p></td></tr>
25-
<tr><td><p>wtimeout_msec</p></td><td><p>A int32_t containing the timeout in milliseconds.</p></td></tr>
25+
<tr><td><p>wtimeout_msec</p></td><td><p>A positive <code>int32_t</code> or zero.</p></td></tr>
2626
</table>
2727
</section>
2828

2929
<section id="description">
3030
<title>Description</title>
31-
<p>Set the timeout in milliseconds that the server should wait before idicating that the write has failed. This is not the same as a socket timeout.</p>
31+
<p>Set the timeout in milliseconds that the server should wait before indicating that the write has failed. This is not the same as a socket timeout. A value of zero may be used to indicate no timeout.</p>
3232
</section>
3333

3434
</page>

src/mongoc/mongoc-write-concern.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,21 @@ mongoc_write_concern_get_wtimeout (const mongoc_write_concern_t *write_concern)
198198
* @wtimeout_msec: Number of milliseconds before timeout.
199199
*
200200
* Sets the number of milliseconds to wait before considering a write
201-
* request as failed.
201+
* request as failed. A value of 0 indicates no write timeout.
202+
*
203+
* The @wtimeout_msec parameter must be positive or zero. Negative values will
204+
* be ignored.
202205
*/
203206
void
204207
mongoc_write_concern_set_wtimeout (mongoc_write_concern_t *write_concern,
205-
int32_t wtimeout_msec)
208+
int32_t wtimeout_msec)
206209
{
207210
BSON_ASSERT (write_concern);
208211

212+
if (wtimeout_msec < 0) {
213+
return;
214+
}
215+
209216
if (!_mongoc_write_concern_warn_frozen(write_concern)) {
210217
write_concern->wtimeout = wtimeout_msec;
211218
}
@@ -227,7 +234,10 @@ mongoc_write_concern_get_wmajority (const mongoc_write_concern_t *write_concern)
227234
*
228235
* Sets the "w" of a write concern to "majority". It is suggested that
229236
* you provide a reasonable @wtimeout_msec to wait before considering the
230-
* write request failed.
237+
* write request failed. A @wtimeout_msec value of 0 indicates no write timeout.
238+
*
239+
* The @wtimeout_msec parameter must be positive or zero. Negative values will
240+
* be ignored.
231241
*/
232242
void
233243
mongoc_write_concern_set_wmajority (mongoc_write_concern_t *write_concern,
@@ -237,7 +247,10 @@ mongoc_write_concern_set_wmajority (mongoc_write_concern_t *write_concern,
237247

238248
if (!_mongoc_write_concern_warn_frozen(write_concern)) {
239249
write_concern->w = MONGOC_WRITE_CONCERN_W_MAJORITY;
240-
write_concern->wtimeout = wtimeout_msec;
250+
251+
if (wtimeout_msec >= 0) {
252+
write_concern->wtimeout = wtimeout_msec;
253+
}
241254
}
242255
}
243256

@@ -415,5 +428,9 @@ _mongoc_write_concern_is_valid (const mongoc_write_concern_t *write_concern)
415428
return false;
416429
}
417430

431+
if (write_concern->wtimeout < 0) {
432+
return false;
433+
}
434+
418435
return true;
419436
}

tests/test-mongoc-write-concern.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,38 @@ test_write_concern_fsync_and_journal_gle_and_validity (void)
207207
mongoc_write_concern_destroy(write_concern);
208208
}
209209

210+
static void
211+
test_write_concern_wtimeout_validity (void)
212+
{
213+
mongoc_write_concern_t *write_concern = mongoc_write_concern_new();
214+
215+
/* Test defaults */
216+
ASSERT(write_concern);
217+
ASSERT(mongoc_write_concern_get_w(write_concern) == MONGOC_WRITE_CONCERN_W_DEFAULT);
218+
ASSERT(mongoc_write_concern_get_wtimeout(write_concern) == 0);
219+
ASSERT(!mongoc_write_concern_get_wmajority(write_concern));
220+
221+
/* mongoc_write_concern_set_wtimeout() ignores invalid wtimeout */
222+
mongoc_write_concern_set_wtimeout(write_concern, -1);
223+
ASSERT(mongoc_write_concern_get_w(write_concern) == MONGOC_WRITE_CONCERN_W_DEFAULT);
224+
ASSERT(mongoc_write_concern_get_wtimeout(write_concern) == 0);
225+
ASSERT(!mongoc_write_concern_get_wmajority(write_concern));
226+
ASSERT(_mongoc_write_concern_is_valid(write_concern));
227+
228+
/* mongoc_write_concern_set_wmajority() ignores invalid wtimeout */
229+
mongoc_write_concern_set_wmajority(write_concern, -1);
230+
ASSERT(mongoc_write_concern_get_w(write_concern) == MONGOC_WRITE_CONCERN_W_MAJORITY);
231+
ASSERT(mongoc_write_concern_get_wtimeout(write_concern) == 0);
232+
ASSERT(mongoc_write_concern_get_wmajority(write_concern));
233+
ASSERT(_mongoc_write_concern_is_valid(write_concern));
234+
235+
/* Manually assigning a negative wtimeout will make the write concern invalid */
236+
write_concern->wtimeout = -1;
237+
ASSERT(!_mongoc_write_concern_is_valid(write_concern));
238+
239+
mongoc_write_concern_destroy(write_concern);
240+
}
241+
210242

211243
void
212244
test_write_concern_install (TestSuite *suite)
@@ -215,4 +247,5 @@ test_write_concern_install (TestSuite *suite)
215247
TestSuite_Add (suite, "/WriteConcern/bson_omits_defaults", test_write_concern_bson_omits_defaults);
216248
TestSuite_Add (suite, "/WriteConcern/bson_includes_false_fsync_and_journal", test_write_concern_bson_includes_false_fsync_and_journal);
217249
TestSuite_Add (suite, "/WriteConcern/fsync_and_journal_gle_and_validity", test_write_concern_fsync_and_journal_gle_and_validity);
250+
TestSuite_Add (suite, "/WriteConcern/wtimeout_validity", test_write_concern_wtimeout_validity);
218251
}

0 commit comments

Comments
 (0)