diff options
author | Luke Chen <luke.chen@mongodb.com> | 2021-10-07 17:44:57 +1100 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-10-07 07:04:01 +0000 |
commit | 58971da1ef93435a9f62bf4708a81713def6e88c (patch) | |
tree | c75ff94f74016177b2eef5f82c76a7d79238e8a5 /src/third_party/wiredtiger/test/cppsuite/tests/csuite_style_example_test.cxx | |
parent | cf4ce26ca21a836aa336313a4a10676ca91c3131 (diff) | |
download | mongo-58971da1ef93435a9f62bf4708a81713def6e88c.tar.gz |
Import wiredtiger: e8bf2e5fcb33197b73c7bb550742926f59e22218 from branch mongodb-4.4r4.4.10-rc0r4.4.10
ref: a9bd69a7ca..e8bf2e5fcb
for: 4.4.10
WT-5270 Create wtperf script for evergreen
WT-7169 Commit ts should not be lesser than the last durable ts in test_timestamp22.py
WT-7392 Added evicted flag to dhandle for use by session sweep
WT-7757 Skip obsolete leaf pages without having to read them
WT-7924 Create a stress test for prefix search near key validation
WT-7980 Create interface to "log:" cursor which switches log files before returning them
WT-7992 Provide API to return the last key in a table regardless of visibility
WT-8022 Verify WT_CURSOR.modify return values in format test program
WT-8036 Added connection panic flag in two assert statements in wt_evict_thread_run and _wt_evict_thread_stop.
WT-8048 Remove split_8 timing stress configuration
WT-8055 Fix issue when compact quits when running at the same time as a checkpoint
WT-8059 Add a check in salvage when no overflow items are found
WT-8068 Improve __rollback_row_modify control flow
WT-8069 Coverity analysis defect 120706: Redundant test
WT-8070 Remove discrepancy between prefix_key and prefix_search
WT-8078 Implement tiered storage local retention caching
WT-8086 Create cpp test for prefix search near entries
WT-8092 Prefix early exit when keys are present in the insert list
WT-8101 Enable diagnostic mode for the CPP tests in evergreen
WT-8115 Define macros only when necessary in cpp files
WT-8119 Add targeted compact testing to the existing testing framework
WT-8146 Stop the tracking component when a CPP test ends
WT-8161 Reduce verbosity of CMake Evergreen smoke
WT-8162 Refactor uses of SMOKE arg in 'define_c_test' helper
WT-8164 Disable rollback_to_stable10 python test on Windows
WT-8171 Implement a C style test in the CPP testing framework
WT-8193 Wrong corner case in VLCS rollback-to-stable
Diffstat (limited to 'src/third_party/wiredtiger/test/cppsuite/tests/csuite_style_example_test.cxx')
-rw-r--r-- | src/third_party/wiredtiger/test/cppsuite/tests/csuite_style_example_test.cxx | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/third_party/wiredtiger/test/cppsuite/tests/csuite_style_example_test.cxx b/src/third_party/wiredtiger/test/cppsuite/tests/csuite_style_example_test.cxx new file mode 100644 index 00000000000..d0059880446 --- /dev/null +++ b/src/third_party/wiredtiger/test/cppsuite/tests/csuite_style_example_test.cxx @@ -0,0 +1,169 @@ +/*- + * Public Domain 2014-present MongoDB, Inc. + * Public Domain 2008-2014 WiredTiger, Inc. + * + * This is free and unencumbered software released into the public domain. + * + * Anyone is free to copy, modify, publish, use, compile, sell, or + * distribute this software, either in source code form or as a compiled + * binary, for any purpose, commercial or non-commercial, and by any + * means. + * + * In jurisdictions that recognize copyright laws, the author or authors + * of this software dedicate any and all copyright interest in the + * software to the public domain. We make this dedication for the benefit + * of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * software under copyright law. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * This file provides an example of how to create a test in C++ using a few features from the + * framework if any. This file can be used as a template for quick testing and/or when stress + * testing is not required. For any stress testing, it is encouraged to use the framework, see + * example_test.cxx and create_script.sh. + */ + +#include "test_harness/connection_manager.h" +#include "test_harness/thread_manager.h" +#include "test_harness/util/api_const.h" +#include "test_harness/util/logger.h" +#include "test_harness/workload/random_generator.h" + +extern "C" { +#include "wiredtiger.h" +} + +using namespace test_harness; + +bool do_inserts = false; +bool do_reads = false; + +void +insert_op(WT_CURSOR *cursor, int key_size, int value_size) +{ + logger::log_msg(LOG_INFO, "called insert_op"); + + /* Insert random data. */ + std::string key, value; + while (do_inserts) { + key = random_generator::instance().generate_random_string(key_size); + value = random_generator::instance().generate_random_string(value_size); + cursor->set_key(cursor, key.c_str()); + cursor->set_value(cursor, value.c_str()); + testutil_check(cursor->insert(cursor)); + } +} + +void +read_op(WT_CURSOR *cursor, int key_size) +{ + logger::log_msg(LOG_INFO, "called read_op"); + + /* Read random data. */ + std::string key; + while (do_reads) { + key = random_generator::instance().generate_random_string(key_size); + cursor->set_key(cursor, key.c_str()); + cursor->search(cursor); + } +} + +int +main(int argc, char *argv[]) +{ + /* Set the program name for error messages. */ + const std::string progname = testutil_set_progname(argv); + + /* Set the tracing level for the logger component. */ + logger::trace_level = LOG_INFO; + + /* Printing some messages. */ + logger::log_msg(LOG_INFO, "Starting " + progname); + logger::log_msg(LOG_ERROR, "This could be an error."); + + /* Create a connection, set the cache size and specify the home directory. */ + const std::string conn_config = std::string(CONNECTION_CREATE) + ",cache_size=500MB"; + const std::string home_dir = std::string(DEFAULT_DIR) + '_' + progname; + connection_manager::instance().create(conn_config, home_dir); + WT_CONNECTION *conn = connection_manager::instance().get_connection(); + + /* Open different sessions. */ + WT_SESSION *insert_session, *read_session; + testutil_check(conn->open_session(conn, nullptr, nullptr, &insert_session)); + testutil_check(conn->open_session(conn, nullptr, nullptr, &read_session)); + + /* Create a collection. */ + const std::string collection_name = "table:my_collection"; + testutil_check( + insert_session->create(insert_session, collection_name.c_str(), DEFAULT_FRAMEWORK_SCHEMA)); + + /* Open different cursors. */ + WT_CURSOR *insert_cursor, *read_cursor; + const std::string cursor_config = ""; + testutil_check(insert_session->open_cursor( + insert_session, collection_name.c_str(), nullptr, cursor_config.c_str(), &insert_cursor)); + testutil_check(read_session->open_cursor( + read_session, collection_name.c_str(), nullptr, cursor_config.c_str(), &read_cursor)); + + /* Store cursors. */ + std::vector<WT_CURSOR *> cursors; + cursors.push_back(insert_cursor); + cursors.push_back(read_cursor); + + /* Insert some data. */ + std::string key = "a"; + const std::string value = "b"; + insert_cursor->set_key(insert_cursor, key.c_str()); + insert_cursor->set_value(insert_cursor, value.c_str()); + testutil_check(insert_cursor->insert(insert_cursor)); + + /* Read some data. */ + key = "b"; + read_cursor->set_key(read_cursor, key.c_str()); + testutil_assert(read_cursor->search(read_cursor) == WT_NOTFOUND); + + key = "a"; + read_cursor->set_key(read_cursor, key.c_str()); + testutil_check(read_cursor->search(read_cursor)); + + /* Create a thread manager and spawn some threads that will work. */ + thread_manager t; + int key_size = 1, value_size = 2; + + do_inserts = true; + t.add_thread(insert_op, insert_cursor, key_size, value_size); + + do_reads = true; + t.add_thread(read_op, read_cursor, key_size); + + /* Sleep for the test duration. */ + int test_duration_s = 5; + std::this_thread::sleep_for(std::chrono::seconds(test_duration_s)); + + /* Stop the threads. */ + do_reads = false; + do_inserts = false; + t.join(); + + /* Close cursors. */ + for (auto c : cursors) + testutil_check(c->close(c)); + + /* Close the connection. */ + connection_manager::instance().close(); + + /* Another message. */ + logger::log_msg(LOG_INFO, "End of test."); + + return (0); +} |