|
1 | 1 | #include "mongoc.h"
|
| 2 | +#include "mongoc-cursor-private.h" |
2 | 3 | #include "mongoc-util-private.h"
|
3 | 4 | #include "mongoc-collection-private.h"
|
4 | 5 | #include "utlist.h"
|
@@ -56,14 +57,14 @@ test_session_no_crypto (void *ctx)
|
56 | 57 | } while (0)
|
57 | 58 |
|
58 | 59 |
|
59 |
| -#define ASSERT_SESSIONS_DIFFER(_lsid_a, _lsid_b) \ |
60 |
| - do { \ |
61 |
| - /* need a match context when checking that lsids DON'T match */ \ |
62 |
| - char errmsg[1000]; \ |
63 |
| - match_ctx_t ctx = {0}; \ |
64 |
| - ctx.errmsg = errmsg; \ |
65 |
| - ctx.errmsg_len = sizeof (errmsg); \ |
66 |
| - BSON_ASSERT (!match_bson_with_ctx ((_lsid_a), (_lsid_b), false, &ctx)); \ |
| 60 | +#define ASSERT_SESSIONS_DIFFER(_lsid_a, _lsid_b) \ |
| 61 | + do { \ |
| 62 | + /* need a match context when checking that lsids DON'T match */ \ |
| 63 | + char errmsg[1000]; \ |
| 64 | + match_ctx_t _ctx = {0}; \ |
| 65 | + _ctx.errmsg = errmsg; \ |
| 66 | + _ctx.errmsg_len = sizeof (errmsg); \ |
| 67 | + BSON_ASSERT (!match_bson_with_ctx ((_lsid_a), (_lsid_b), false, &_ctx)); \ |
67 | 68 | } while (0)
|
68 | 69 |
|
69 | 70 |
|
@@ -1917,6 +1918,65 @@ test_find_indexes (session_test_t *test)
|
1917 | 1918 | }
|
1918 | 1919 |
|
1919 | 1920 |
|
| 1921 | +#define ASSERT_POOL_SIZE(_topology, _expected_size) \ |
| 1922 | + do { \ |
| 1923 | + const mongoc_server_session_t *_tmp; \ |
| 1924 | + int _n_sessions; \ |
| 1925 | + CDL_COUNT ((_topology)->session_pool, _tmp, _n_sessions); \ |
| 1926 | + ASSERT_CMPINT (_n_sessions, ==, (int) (_expected_size)); \ |
| 1927 | + } while (0) |
| 1928 | + |
| 1929 | + |
| 1930 | +static void |
| 1931 | +test_cursor_implicit_session (void *ctx) |
| 1932 | +{ |
| 1933 | + session_test_t *test; |
| 1934 | + mongoc_topology_t *topology; |
| 1935 | + mongoc_cursor_t *cursor; |
| 1936 | + const bson_t *doc; |
| 1937 | + mongoc_client_session_t *cs; |
| 1938 | + bson_t find_lsid; |
| 1939 | + bson_error_t error; |
| 1940 | + |
| 1941 | + test = session_test_new (CORRECT_CLIENT, NOT_CAUSAL); |
| 1942 | + test->expect_explicit_lsid = false; |
| 1943 | + topology = test->client->topology; |
| 1944 | + cs = mongoc_client_start_session (test->client, NULL, &error); |
| 1945 | + ASSERT_OR_PRINT (cs, error); |
| 1946 | + |
| 1947 | + mongoc_collection_drop_with_opts (test->session_collection, NULL, NULL); |
| 1948 | + insert_10_docs (test); |
| 1949 | + cursor = mongoc_collection_find_with_opts ( |
| 1950 | + test->collection, tmp_bson ("{}"), &test->opts, NULL); |
| 1951 | + |
| 1952 | + BSON_ASSERT (!cursor->client_session); |
| 1953 | + mongoc_cursor_set_batch_size (cursor, 2); |
| 1954 | + |
| 1955 | + /* start the cursor. it makes an implicit session & sends it with "find" */ |
| 1956 | + BSON_ASSERT (mongoc_cursor_next (cursor, &doc)); |
| 1957 | + BSON_ASSERT (cursor->client_session); |
| 1958 | + BSON_ASSERT (!cursor->explicit_session); |
| 1959 | + bson_copy_to (&cursor->client_session->server_session->lsid, &find_lsid); |
| 1960 | + ASSERT_POOL_SIZE (topology, 0); |
| 1961 | + ASSERT_SESSIONS_MATCH (&test->sent_lsid, &find_lsid); |
| 1962 | + |
| 1963 | + /* push a new server session into the pool */ |
| 1964 | + mongoc_client_session_destroy (cs); |
| 1965 | + ASSERT_POOL_SIZE (topology, 1); |
| 1966 | + ASSERT_SESSIONS_DIFFER (&find_lsid, &topology->session_pool->lsid); |
| 1967 | + |
| 1968 | + /* "getMore" uses the same lsid as "find" did */ |
| 1969 | + bson_reinit (&test->sent_lsid); |
| 1970 | + ASSERT_CURSOR_COUNT (9, cursor); |
| 1971 | + ASSERT_SESSIONS_MATCH (&test->sent_lsid, &find_lsid); |
| 1972 | + ASSERT_OR_PRINT (!mongoc_cursor_error (cursor, &error), error); |
| 1973 | + |
| 1974 | + bson_destroy (&find_lsid); |
| 1975 | + mongoc_cursor_destroy (cursor); |
| 1976 | + session_test_destroy (test); |
| 1977 | +} |
| 1978 | + |
| 1979 | + |
1920 | 1980 | static void
|
1921 | 1981 | test_cmd_error (void *ctx)
|
1922 | 1982 | {
|
@@ -2247,6 +2307,13 @@ test_session_install (TestSuite *suite)
|
2247 | 2307 | suite, "/Session/collection_names", test_collection_names, true);
|
2248 | 2308 | add_session_test (suite, "/Session/bulk", test_bulk, false);
|
2249 | 2309 | add_session_test (suite, "/Session/find_indexes", test_find_indexes, true);
|
| 2310 | + TestSuite_AddFull (suite, |
| 2311 | + "/Session/cursor_implicit_session", |
| 2312 | + test_cursor_implicit_session, |
| 2313 | + NULL, |
| 2314 | + NULL, |
| 2315 | + test_framework_skip_if_no_cluster_time, |
| 2316 | + test_framework_skip_if_no_crypto); |
2250 | 2317 | TestSuite_AddFull (suite,
|
2251 | 2318 | "/Session/cmd_error",
|
2252 | 2319 | test_cmd_error,
|
|
0 commit comments