1 /**
2  * @file   unit_test_config.h
3  *
4  * @section LICENSE
5  *
6  * The MIT License
7  *
8  * @copyright Copyright (c) 2018-2021 TileDB, Inc.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  *
28  * @section DESCRIPTION
29  *
30  * This file declares the UnitTestConfig class.
31  */
32 
33 #ifndef TILEDB_UNIT_TEST_CONFIG_H
34 #define TILEDB_UNIT_TEST_CONFIG_H
35 
36 #include <cassert>
37 
38 #include "tiledb/common/macros.h"
39 
40 using namespace tiledb::common;
41 
42 namespace tiledb {
43 namespace sm {
44 
45 /**
46  * A global singleton for communication between unit tests and their classes
47  * under test.
48  */
49 class UnitTestConfig {
50  public:
51   /** Singleton instance. Thread-safe. */
instance()52   static UnitTestConfig& instance() {
53     static UnitTestConfig self;
54     return self;
55   }
56 
57   /** Wraps an attribute type to determine if it has been intentionally set. */
58   template <typename T>
59   class Attribute {
60    public:
61     /** Constructor. */
Attribute()62     Attribute()
63         : set_(false) {
64     }
65 
66     /** Destructor. */
67     ~Attribute() = default;
68 
69     /**
70      * Returns true if the internal attribute has been set.
71      *
72      * @return bool
73      */
is_set()74     bool is_set() const {
75       return set_;
76     }
77 
78     /**
79      * Sets the internal attribute.
80      *
81      * @param T the internal attribute value to set.
82      */
set(const T & attr)83     void set(const T& attr) {
84       attr_ = attr;
85       set_ = true;
86     }
87 
88     /**
89      * Sets the internal attribute.
90      *
91      * @param T the internal attribute value to set.
92      */
set(T && attr)93     void set(T&& attr) {
94       attr_ = std::move(attr);
95       set_ = true;
96     }
97 
98     /**
99      * Unsets the internal attribute.
100      */
reset()101     void reset() {
102       set_ = false;
103     }
104 
105     /**
106      * Returns value of the internal attribute.
107      *
108      * @return T the internal attribute value.
109      */
get()110     T get() const {
111       assert(set_);
112       return attr_;
113     }
114 
115    private:
116     DISABLE_COPY_AND_COPY_ASSIGN(Attribute);
117     DISABLE_MOVE_AND_MOVE_ASSIGN(Attribute);
118 
119     /** True if 'attr_' has been set. */
120     bool set_;
121 
122     /** Internal attribute. */
123     T attr_;
124   };
125 
126   /** For every nth multipart upload request, return a non-OK status. */
127   Attribute<unsigned int> s3_fail_every_nth_upload_request;
128 
129   /** Used when setting a non-standard key_length. */
130   Attribute<uint32_t> array_encryption_key_length;
131 
132  private:
133   /** Constructor. */
134   UnitTestConfig() = default;
135 
136   /** Destructor. */
137   ~UnitTestConfig() = default;
138 
139   DISABLE_COPY_AND_COPY_ASSIGN(UnitTestConfig);
140   DISABLE_MOVE_AND_MOVE_ASSIGN(UnitTestConfig);
141 };
142 
143 }  // namespace sm
144 }  // namespace tiledb
145 
146 #endif  // TILEDB_UNIT_TEST_CONFIG_H
147