diff --git a/lib/spdylay_session.c b/lib/spdylay_session.c index 58c7b6e5..e335c7e0 100644 --- a/lib/spdylay_session.c +++ b/lib/spdylay_session.c @@ -358,13 +358,13 @@ int spdylay_session_close_stream(spdylay_session *session, int32_t stream_id, { spdylay_stream *stream = spdylay_session_get_stream(session, stream_id); if(stream) { - spdylay_map_erase(&session->streams, stream_id); if(stream->state != SPDYLAY_STREAM_INITIAL && session->callbacks.on_stream_close_callback) { session->callbacks.on_stream_close_callback(session, stream_id, status_code, session->user_data); } + spdylay_map_erase(&session->streams, stream_id); spdylay_stream_free(stream); free(stream); return 0; diff --git a/tests/main.c b/tests/main.c index a5adfafa..8ba067c1 100644 --- a/tests/main.c +++ b/tests/main.c @@ -109,6 +109,8 @@ int main(int argc, char* argv[]) test_spdylay_session_pop_next_ob_item) || !CU_add_test(pSuite, "session_on_request_recv_callback", test_spdylay_session_on_request_recv_callback) || + !CU_add_test(pSuite, "session_on_stream_close", + test_spdylay_session_on_stream_close) || !CU_add_test(pSuite, "frame_unpack_nv", test_spdylay_frame_unpack_nv) || !CU_add_test(pSuite, "frame_count_nv_space", test_spdylay_frame_count_nv_space) || diff --git a/tests/spdylay_session_test.c b/tests/spdylay_session_test.c index fb18fb71..49e52236 100644 --- a/tests/spdylay_session_test.c +++ b/tests/spdylay_session_test.c @@ -943,6 +943,38 @@ void test_spdylay_session_on_request_recv_callback() CU_ASSERT(0 == user_data.stream_id); spdylay_frame_headers_free(&frame.headers); - + spdylay_session_del(session); +} + +void stream_close_callback(spdylay_session *session, int32_t stream_id, + spdylay_status_code status_code, void *user_data) +{ + my_user_data* my_data = (my_user_data*)user_data; + void *stream_data = spdylay_session_get_stream_user_data(session, stream_id); + my_data->invalid = 1; + my_data->valid = stream_data != NULL ? 1 : 0; +} + +void test_spdylay_session_on_stream_close() +{ + spdylay_session *session; + spdylay_session_callbacks callbacks; + my_user_data user_data; + spdylay_stream *stream; + int32_t stream_id = 1; + uint8_t pri = 3; + + memset(&callbacks, 0, sizeof(spdylay_session_callbacks)); + callbacks.on_stream_close_callback = stream_close_callback; + user_data.valid = 0; + user_data.invalid = 0; + + CU_ASSERT(spdylay_session_client_new(&session, &callbacks, &user_data) == 0); + stream = spdylay_session_open_stream(session, stream_id, SPDYLAY_FLAG_NONE, + pri, SPDYLAY_STREAM_OPENED, &user_data); + CU_ASSERT(stream != NULL); + CU_ASSERT(spdylay_session_close_stream(session, stream_id, SPDYLAY_OK) == 0); + CU_ASSERT(user_data.invalid == 1); /* Called the callback. */ + CU_ASSERT(user_data.valid == 1); spdylay_session_del(session); } diff --git a/tests/spdylay_session_test.h b/tests/spdylay_session_test.h index f0edb697..be29fcfd 100644 --- a/tests/spdylay_session_test.h +++ b/tests/spdylay_session_test.h @@ -46,5 +46,6 @@ void test_spdylay_session_send_rst_stream(); void test_spdylay_session_get_next_ob_item(); void test_spdylay_session_pop_next_ob_item(); void test_spdylay_session_on_request_recv_callback(); +void test_spdylay_session_on_stream_close(); #endif // SPDYLAY_SESSION_TEST_H