PROTON-2338:: Added some testing for raw connection half close
Unit test half close
Half close write side in raw_connect when writing final message
diff --git a/c/examples/raw_connect.c b/c/examples/raw_connect.c
index 36f2f71..d380801 100644
--- a/c/examples/raw_connect.c
+++ b/c/examples/raw_connect.c
@@ -143,6 +143,7 @@
pn_raw_connection_set_context(c, NULL);
free(cd);
printf("**raw connection disconnected\n");
+ pn_proactor_cancel_timeout(app->proactor);
app->disconnects++;
check_condition(event, pn_raw_connection_condition(c), app);
} break;
@@ -153,7 +154,8 @@
if (fgets(line, sizeof(line), stdin)) {
send_message(c, line);
} else {
- /* On end of file wait 2 sec for response */
+ /* On end of file, close for write then wait 2 sec for response */
+ pn_raw_connection_write_close(c);
app->towake = c;
pn_proactor_set_timeout(app->proactor, 2000);
}
diff --git a/c/examples/raw_echo.c b/c/examples/raw_echo.c
index 374ffaa..d47f95b 100644
--- a/c/examples/raw_echo.c
+++ b/c/examples/raw_echo.c
@@ -96,7 +96,11 @@
buffer.capacity = 1024;
buffer.offset = 0;
buffer.size = len;
- pn_raw_connection_write_buffers(c, &buffer, 1);
+ // If message not accepted just throw it away!
+ if (pn_raw_connection_write_buffers(c, &buffer, 1) < 1) {
+ printf("**Couldn't send message: write not accepted**\n");
+ free(buf);
+ }
}
static void recv_message(pn_raw_buffer_t buf) {
diff --git a/c/tests/raw_connection_test.cpp b/c/tests/raw_connection_test.cpp
index 4ad4689..b7ce803 100644
--- a/c/tests/raw_connection_test.cpp
+++ b/c/tests/raw_connection_test.cpp
@@ -530,7 +530,7 @@
// No need buffers event as we already gave buffers
REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_EVENT_NONE);
- pni_raw_close(p);
+ pni_raw_write_close(p);
size_t rgiven = pn_raw_connection_take_read_buffers(p, &read[0], rtaken);
REQUIRE(pni_raw_validate(p));
@@ -539,19 +539,35 @@
REQUIRE(pni_raw_validate(p));
CHECK(wgiven==0);
- REQUIRE(pn_raw_connection_is_read_closed(p));
REQUIRE(pn_raw_connection_is_write_closed(p));
- REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_RAW_CONNECTION_CLOSED_READ);
- REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_EVENT_NONE);
-
- REQUIRE_FALSE(pni_raw_can_read(p));
REQUIRE(pni_raw_can_write(p));
pni_raw_write(p, fds[0], snd, set_write_error);
REQUIRE(pni_raw_validate(p));
CHECK(write_err == 0);
+ REQUIRE_FALSE(pni_raw_can_write(p));
+
+ REQUIRE(pni_raw_can_read(p));
+ pni_raw_read(p, fds[0], rcv, set_read_error);
+ REQUIRE(pni_raw_validate(p));
+ CHECK(read_err == 0);
+
REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_RAW_CONNECTION_WRITTEN);
+
+ SECTION("Read close after write close") {
+ pni_raw_read_close(p);
+ }
+
+ SECTION("Full close after write close") {
+ // We should be able to fully close here (even if we read close would be more specific)
+ pni_raw_close(p);
+ }
+
+ REQUIRE(pn_raw_connection_is_read_closed(p));
+ REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_RAW_CONNECTION_CLOSED_READ);
+
+ REQUIRE_FALSE(pni_raw_can_read(p));
REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_RAW_CONNECTION_CLOSED_WRITE);
REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_RAW_CONNECTION_READ);
rgiven = pn_raw_connection_take_read_buffers(p, &read[0], rtaken);
@@ -561,6 +577,10 @@
wgiven = pn_raw_connection_take_written_buffers(p, &written[0], wtaken);
REQUIRE(pni_raw_validate(p));
CHECK(wgiven==wtaken);
+
+ // This should have no affect because we are already read and write closed
+ pni_raw_close(p);
+
REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_RAW_CONNECTION_DISCONNECTED);
REQUIRE(pn_event_type(pni_raw_event_next(p)) == PN_EVENT_NONE);
@@ -770,7 +790,6 @@
REQUIRE(pni_raw_validate(p));
CHECK(rgiven > 0);
- CHECK(pn_raw_connection_read_buffers_capacity(p) == rgiven);
CHECK(read[rgiven-1].size == 0);
// At this point we should have read everything - make sure it matches