~mario-brothers/libmario/ubuntu

« back to all changes in this revision

Viewing changes to tests/logging/configuration.cpp

  • Committer: Thomas Voß
  • Date: 2012-08-15 11:00:53 UTC
  • Revision ID: thomas.voss@canonical.com-20120815110053-83zzyobpxxr1bg3b
Added copyright headers and synchronous/asynchronous default configurations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
/*
 
3
 * Copyright 2012 Canonical Ltd.
 
4
 *
 
5
 * This program is free software: you can redistribute it and/or modify it
 
6
 * under the terms of the GNU Lesser General Public License, as
 
7
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 
8
 * of the License.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
12
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 
13
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 
14
 * License for more details.
 
15
 *
 
16
 * You should have received a copy of both the GNU Lesser General Public
 
17
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 
18
 *
 
19
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
20
 *
 
21
 */
 
22
#include "mario/logging/configuration.h"
 
23
#include "mario/logging/logging.h"
 
24
#include "mario/logging/asynchronous_configuration.h"
 
25
#include "mario/logging/synchronous_configuration.h"
 
26
#include "mario/logging/writer.h"
 
27
#include "mario/logging/backend/async_backend.h"
 
28
#include "mario/logging/backend/output_stream_backend.h"
 
29
#include "mario/logging/backend/syslog_backend.h"
 
30
 
 
31
#include "environment.h"
 
32
 
 
33
#include <gmock/gmock.h>
 
34
#include <gtest/gtest.h>
 
35
 
 
36
#include <boost/asio/io_service.hpp>
 
37
#include <boost/bind.hpp>
 
38
#include <boost/thread.hpp>
 
39
 
 
40
namespace ml = mario::logging;
 
41
namespace mlt = mario::logging::testing;
 
42
 
 
43
namespace
 
44
{
 
45
void setup_env()
 
46
{
 
47
    static const int do_overwrite = 1;
 
48
 
 
49
    ::setenv(
 
50
        ml::Configuration::default_log_level_env().c_str(),
 
51
        "INFO",
 
52
        do_overwrite);
 
53
}
 
54
 
 
55
struct MockConfiguration : public ml::Configuration
 
56
{
 
57
    MockConfiguration() {}
 
58
 
 
59
    MOCK_METHOD0(create_default_writer, ml::Writer*());
 
60
    MOCK_METHOD0(create_syslog_writer, ml::Writer*());
 
61
};
 
62
 
 
63
}
 
64
 
 
65
TEST(
 
66
    LoggingConfiguration, 
 
67
    a_configuration_can_be_initialized_from_environment_and_from_command_line)
 
68
{
 
69
    setup_env();
 
70
    
 
71
    std::shared_ptr<ml::Configuration> config = ml::default_synchronous_configuration();
 
72
    EXPECT_EQ(
 
73
        ml::Configuration::updated, 
 
74
        config->update_from_environment());
 
75
    EXPECT_EQ(
 
76
        ml::Info,
 
77
        config->default_log_level());
 
78
    EXPECT_EQ(
 
79
        ml::Configuration::updated,
 
80
        config->update_from_command_line(mlt::Environment::argc(), mlt::Environment::argv()));
 
81
    EXPECT_EQ(
 
82
        ml::Debug,
 
83
        config->default_log_level());
 
84
    EXPECT_EQ(
 
85
        mlt::Environment::argv()[0],
 
86
        config->executable_name());
 
87
    
 
88
}
 
89
 
 
90
namespace
 
91
{
 
92
struct NullDeleter
 
93
{
 
94
    template<typename T>
 
95
    void operator()(T*) const
 
96
    {
 
97
    }
 
98
};
 
99
}
 
100
 
 
101
TEST(
 
102
    LoggingInitialization,
 
103
    initializing_the_library_calls_create_loggers)
 
104
{
 
105
    using namespace ::testing;
 
106
 
 
107
    MockConfiguration mock;
 
108
    std::shared_ptr<ml::Configuration> config(&mock, NullDeleter());
 
109
    EXPECT_CALL(mock, create_default_writer()).Times(AtLeast(1));
 
110
    EXPECT_CALL(mock, create_syslog_writer()).Times(AtLeast(1));
 
111
    
 
112
    ml::init_for_configuration(config);
 
113
}
 
114
 
 
115
TEST(
 
116
    DefaultSynchronousConfiguration,
 
117
    initializing_with_a_default_synchronous_configuration_sets_up_global_writers)
 
118
{
 
119
    std::shared_ptr<ml::Configuration> config = ml::default_synchronous_configuration();
 
120
    ml::init_for_configuration(config);
 
121
 
 
122
    ml::Writer::default_instance();
 
123
    ml::Writer::syslog_instance();
 
124
}
 
125
 
 
126
TEST(
 
127
    DefaultAsynchronousConfiguration,
 
128
    initializing_with_a_default_asynchronous_configuration_sets_up_global_writers)
 
129
{
 
130
    std::shared_ptr<ml::Configuration> config = ml::default_asynchronous_configuration();
 
131
    ml::init_for_configuration(config);
 
132
 
 
133
    ml::Writer::default_instance();
 
134
    ml::Writer::syslog_instance();
 
135
}
 
136
 
 
137
TEST(
 
138
    DefaultAsynchronousConfiguration,
 
139
    initializing_with_threading_hint_to_start_own_thread_switches_config_to_state_running)
 
140
{
 
141
    auto async_config = ml::default_asynchronous_configuration();
 
142
 
 
143
    boost::this_thread::sleep(boost::posix_time::milliseconds(10));
 
144
 
 
145
    EXPECT_TRUE(async_config->is_running());
 
146
}
 
147
 
 
148
TEST(
 
149
    DefaultAsynchronousConfiguration,
 
150
    initializing_with_threading_hint_to_not_start_own_thread_does_not_switch_config_automatically)
 
151
{
 
152
    auto async_config = ml::default_asynchronous_configuration(
 
153
        ml::AsynchronousConfiguration::do_not_run_in_own_thread);
 
154
 
 
155
    EXPECT_FALSE(async_config->is_running());
 
156
 
 
157
    boost::thread t(
 
158
        boost::bind(
 
159
            &ml::AsynchronousConfiguration::run,
 
160
            async_config.get()));
 
161
 
 
162
    boost::this_thread::sleep(boost::posix_time::milliseconds(10));
 
163
 
 
164
    EXPECT_TRUE(async_config->is_running());
 
165
 
 
166
    async_config->stop();
 
167
    t.join();
 
168
    
 
169
    EXPECT_FALSE(async_config->is_running());    
 
170
}