Storage Engine API
test_harness_helper.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <cstdint>
32 #include <initializer_list>
33 #include <memory>
34 
35 #include "mongo/db/jsobj.h"
36 #include "mongo/db/operation_context_noop.h"
37 #include "mongo/db/record_id.h"
38 #include "mongo/db/service_context.h"
39 #include "mongo/db/service_context_noop.h"
41 #include "mongo/stdx/functional.h"
42 #include "mongo/stdx/memory.h"
43 #include "mongo/util/unowned_ptr.h"
44 
45 namespace mongo {
46 
56 public:
57  virtual ~HarnessHelper() = 0;
58 
59  explicit HarnessHelper() = default;
60 
61  virtual ServiceContext::UniqueOperationContext newOperationContext(Client* const client) {
62  auto opCtx = client->makeOperationContext();
63  opCtx->setRecoveryUnit(newRecoveryUnit().release(),
64  WriteUnitOfWork::RecoveryUnitState::kNotInUnitOfWork);
65  return opCtx;
66  }
67 
68  virtual ServiceContext::UniqueOperationContext newOperationContext() {
69  return newOperationContext(_client.get());
70  }
71 
72  Client* client() const {
73  return _client.get();
74  }
75 
76  ServiceContext* serviceContext() {
77  return &_serviceContext;
78  }
79 
80  const ServiceContext* serviceContext() const {
81  return &_serviceContext;
82  }
83 
84  virtual std::unique_ptr<RecoveryUnit> newRecoveryUnit() = 0;
85 
86 private:
87  ServiceContextNoop _serviceContext;
88  ServiceContext::UniqueClient _client = _serviceContext.makeClient("hh");
89 };
90 
91 namespace harness_helper_detail {
92 template <typename Target, typename Current>
93 std::unique_ptr<Target> noexcept_ptr_conversion(std::unique_ptr<Current>&& p, Target& t) noexcept {
94  p.release();
95  return std::unique_ptr<Target>(std::addressof(t));
96 }
97 } // namespace harness_helper_detail
98 
99 extern void registerHarnessHelperFactory(stdx::function<std::unique_ptr<HarnessHelper>()> factory);
100 
101 template <typename Target, typename Current>
102 std::unique_ptr<Target> dynamic_ptr_cast(std::unique_ptr<Current>&& p) {
103  if (!p) {
104  throw std::runtime_error("Must not be null.");
105  }
106  Target& target = dynamic_cast<Target&>(*p);
107  return harness_helper_detail::noexcept_ptr_conversion(std::move(p), target);
108 }
109 
110 std::unique_ptr<HarnessHelper> newHarnessHelper();
111 } // namespace mongo
virtual ServiceContext::UniqueOperationContext newOperationContext()
Definition: test_harness_helper.h:68
std::unique_ptr< Target > dynamic_ptr_cast(std::unique_ptr< Current > &&p)
Definition: test_harness_helper.h:102
void registerHarnessHelperFactory(stdx::function< std::unique_ptr< HarnessHelper >()> factory)
Definition: test_harness_helper.cpp:45
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
Client * client() const
Definition: test_harness_helper.h:72
const ServiceContext * serviceContext() const
Definition: test_harness_helper.h:80
ServiceContext::UniqueClient _client
Definition: test_harness_helper.h:88
Sets up an OperationContext with a Recovery Unit.
Definition: test_harness_helper.h:55
virtual ~HarnessHelper()=0
std::unique_ptr< HarnessHelper > newHarnessHelper()
Definition: test_harness_helper.cpp:49
virtual ServiceContext::UniqueOperationContext newOperationContext(Client *const client)
Definition: test_harness_helper.h:61
ServiceContextNoop _serviceContext
Definition: test_harness_helper.h:87
ServiceContext * serviceContext()
Definition: test_harness_helper.h:76
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
std::unique_ptr< Target > noexcept_ptr_conversion(std::unique_ptr< Current > &&p, Target &t) noexcept
Definition: test_harness_helper.h:93
virtual std::unique_ptr< RecoveryUnit > newRecoveryUnit()=0