|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include "mesh/concurrency.h" |
| 3 | + |
| 4 | +using namespace mesh; |
| 5 | + |
| 6 | +class test_class { |
| 7 | + public: |
| 8 | + context::Context _ctx; |
| 9 | + |
| 10 | + void init(context::Context &ctx) { _ctx = context::WithCancel(ctx); } |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test designed to detect memory leaks in the Context class. |
| 15 | + * |
| 16 | + * Run the test with Valgrind to detect memory leaks. |
| 17 | + * valgrind --leak-check=full ./media_proxy_unit_tests --gtest_filter=*Context* |
| 18 | + */ |
| 19 | +TEST(MeshContext, constructor) { |
| 20 | + auto ctx = context::WithCancel(context::Background()); |
| 21 | + test_class t; |
| 22 | + t.init(ctx); |
| 23 | + { |
| 24 | + auto ctx2 = context::WithCancel(ctx); |
| 25 | + t.init(ctx2); |
| 26 | + mesh::thread::Sleep(ctx2, std::chrono::milliseconds(10)); |
| 27 | + ctx2.cancel(); |
| 28 | + } |
| 29 | + { |
| 30 | + context::Context c1; |
| 31 | + c1 = context::Context(); |
| 32 | + } |
| 33 | + { |
| 34 | + context::Context c1 = context::WithTimeout(ctx, std::chrono::milliseconds(10)); |
| 35 | + context::Context c2 = context::WithTimeout(c1, std::chrono::milliseconds(20)); |
| 36 | + context::Context c3 = context::WithTimeout(c2, std::chrono::milliseconds(30)); |
| 37 | + context::Context c4 = context::WithTimeout(c3, std::chrono::milliseconds(40)); |
| 38 | + context::Context c5 = context::WithTimeout(c4, std::chrono::milliseconds(50)); |
| 39 | + context::Context c6 = context::WithTimeout(c5, std::chrono::milliseconds(60)); |
| 40 | + mesh::thread::Sleep(c6, std::chrono::milliseconds(10)); |
| 41 | + } |
| 42 | + { |
| 43 | + context::Context c1 = context::WithTimeout(ctx, std::chrono::milliseconds(60)); |
| 44 | + context::Context c2 = context::WithTimeout(c1, std::chrono::milliseconds(50)); |
| 45 | + context::Context c3 = context::WithTimeout(c2, std::chrono::milliseconds(40)); |
| 46 | + context::Context c4 = context::WithTimeout(c3, std::chrono::milliseconds(30)); |
| 47 | + context::Context c5 = context::WithTimeout(c4, std::chrono::milliseconds(20)); |
| 48 | + context::Context c6 = context::WithTimeout(c5, std::chrono::milliseconds(10)); |
| 49 | + mesh::thread::Sleep(c6, std::chrono::milliseconds(10)); |
| 50 | + } |
| 51 | +} |
0 commit comments