diff --git a/lib/spdylay_session.c b/lib/spdylay_session.c index f0b22e2a..ada02052 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 006f24d9..5e56beeb 100644 --- a/tests/main.c +++ b/tests/main.c @@ -107,6 +107,8 @@ int main(int argc, char* argv[]) test_spdylay_session_get_next_ob_item) || !CU_add_test(pSuite, "session_pop_next_ob_item", test_spdylay_session_pop_next_ob_item) || + !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 a67a9003..59ddd0e1 100644 --- a/tests/spdylay_session_test.c +++ b/tests/spdylay_session_test.c @@ -883,3 +883,37 @@ void test_spdylay_session_pop_next_ob_item() 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; + const char *nv[] = { NULL }; + 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 66efd7bb..8385a2db 100644 --- a/tests/spdylay_session_test.h +++ b/tests/spdylay_session_test.h @@ -45,5 +45,6 @@ void test_spdylay_session_is_my_stream_id(); 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_stream_close(); #endif // SPDYLAY_SESSION_TEST_H