summaryrefslogtreecommitdiff
path: root/src/mongo/util/background_job_test.cpp
diff options
context:
space:
mode:
authorAndrew Morrow <acm@10gen.com>2013-10-31 15:34:38 -0400
committerAndrew Morrow <acm@10gen.com>2013-11-02 10:19:06 -0400
commitcc2e41be23c7052f28aa985b99e0c03a84017593 (patch)
tree1a980b69a503b85c04e2d052f6dc25e1ae9f2637 /src/mongo/util/background_job_test.cpp
parent50030f55d08d5ffe6a10e76542487b1b989e4972 (diff)
downloadmongo-cc2e41be23c7052f28aa985b99e0c03a84017593.tar.gz
SERVER-7217 Provide new client API to manage initialization and termination
Diffstat (limited to 'src/mongo/util/background_job_test.cpp')
-rw-r--r--src/mongo/util/background_job_test.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/mongo/util/background_job_test.cpp b/src/mongo/util/background_job_test.cpp
new file mode 100644
index 00000000000..f7698ae0d4a
--- /dev/null
+++ b/src/mongo/util/background_job_test.cpp
@@ -0,0 +1,90 @@
+/* Copyright 2013 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/db/server_options.h"
+#include "mongo/unittest/unittest.h"
+#include "mongo/util/background.h"
+#include "mongo/util/concurrency/mutex.h"
+#include "mongo/util/concurrency/synchronization.h"
+
+namespace mongo {
+
+ ServerGlobalParams serverGlobalParams;
+
+ bool inShutdown() {
+ return false;
+ }
+
+} // namespace mongo
+
+namespace {
+
+ using mongo::BackgroundJob;
+ using mongo::MsgAssertionException;
+ using mongo::mutex;
+ using mongo::Notification;
+
+ TEST(BackgroundJobLifeCycle, Go) {
+
+ class Job : public BackgroundJob {
+ public:
+ Job()
+ : _mutex("BackgroundJobLifeCycle::Go")
+ , _hasRun(false) {}
+
+ virtual std::string name() const {
+ return "BackgroundLifeCycle::CannotCallGoAgain";
+ }
+
+ virtual void run() {
+ {
+ mongo::scoped_lock lock( _mutex );
+ ASSERT_FALSE( _hasRun );
+ _hasRun = true;
+ }
+
+ _n.waitToBeNotified();
+ }
+
+ void notify() {
+ _n.notifyOne();
+ }
+
+ private:
+ mutex _mutex;
+ bool _hasRun;
+ Notification _n;
+ };
+
+ Job j;
+
+ // This call starts Job running.
+ j.go();
+
+ // Calling 'go' again while it is running is an error.
+ ASSERT_THROWS(j.go(), MsgAssertionException);
+
+ // Stop the Job
+ j.notify();
+ j.wait();
+
+ // Calling 'go' on a done task is a no-op. If it were not,
+ // we would fail the assert in Job::run above.
+ j.go();
+ }
+
+} // namespace