summaryrefslogtreecommitdiff
path: root/dbtests/repltests.cpp
blob: 14732c8a8419faf86dd51c7e52242ca2c90f4be0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// repltests.cpp : Unit tests for replication
//

/**
 *    Copyright (C) 2009 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../db/repl.h"

#include "../db/db.h"
#include "../db/instance.h"
#include "../db/json.h"

#include "dbtests.h"

namespace mongo {
    void createOplog();
}

namespace ReplTests {

    class Base {
    public:
        Base() {
            master = true;
            createOplog();
            dblock lk;
            setClient( ns() );
        }
        ~Base() {
            try {
                master = false;
                deleteAll( ns() );
                deleteAll( logNs() );
            } catch ( ... ) {
                FAIL( "Exception while cleaning up test" );
            }
        }
    protected:
        static const char *ns() {
            return "dbtests.repltests";
        }
        static const char *logNs() {
            return "local.oplog.$main";
        }
        DBDirectClient *client() const { return &client_; }
        BSONObj one( const BSONObj &query = emptyObj ) const {
            return client()->findOne( ns(), query );            
        }
        void checkOne( const BSONObj &o ) const {
            ASSERT( !one( o ).woCompare( o ) );
        }
        void checkAll( const BSONObj &o ) const {
            auto_ptr< DBClientCursor > c = client()->query( ns(), o );
            assert( c->more() );
            while( c->more() ) {
                ASSERT( !o.woCompare( c->next() ) );
            }
        }
        BSONObj oneOp() const { 
            return client()->findOne( logNs(), emptyObj );
        }
        int count() const {
            int count = 0;
            dblock lk;
            setClient( ns() );
            auto_ptr< Cursor > c = theDataFileMgr.findAll( ns() );
            for(; c->ok(); c->advance(), ++count );
            return count;
        }
        static void applyAllOperations() {
            class Applier : public ReplSource {
            public:
                static void apply( const BSONObj &op ) {
                    ReplSource::applyOperation( op );
                }
            };
            dblock lk;
            setClient( logNs() );
            vector< BSONObj > ops;
            for( auto_ptr< Cursor > c = theDataFileMgr.findAll( logNs() ); c->ok(); c->advance() )
                ops.push_back( c->current() );
            setClient( ns() );
            for( vector< BSONObj >::iterator i = ops.begin(); i != ops.end(); ++i )
                Applier::apply( *i );
        }
        // These deletes don't get logged.
        static void deleteAll( const char *ns ) {
            dblock lk;
            setClient( ns );
            auto_ptr< Cursor > c = theDataFileMgr.findAll( ns );
            vector< DiskLoc > toDelete;
            for(; c->ok(); c->advance() ) {
                toDelete.push_back( c->currLoc() );
            }
            for( vector< DiskLoc >::iterator i = toDelete.begin(); i != toDelete.end(); ++i ) {
                theDataFileMgr.deleteRecord( ns, i->rec(), *i, true );            
            }
        }
        static void insert( const BSONObj &o ) {
            dblock lk;
            setClient( ns() );
            theDataFileMgr.insert( ns(), o.objdata(), o.objsize() );
        }
    private:
        static DBDirectClient client_;
    };
    DBDirectClient Base::client_;
    
    class LogBasic : public Base {
    public:
        void run() {
            ASSERT( !database->haveLogged() );
            ASSERT( oneOp().isEmpty() );
            
            client()->insert( ns(), fromjson( "{\"a\":\"b\"}" ) );

            ASSERT( database->haveLogged() );
            ASSERT( !oneOp().isEmpty() );
        }
    };
    
    namespace Idempotence {
        
        class Base : public ReplTests::Base {
        public:
            void run() {
                reset();
                doIt();
                check();
                applyAllOperations();
                check();
                
                reset();
                applyAllOperations();
                check();
                applyAllOperations();
                check();
            }
        protected:
            virtual void doIt() const = 0;
            virtual void check() const = 0;
            virtual void reset() const = 0;
        };
        
        class Insert : public Base {
        public:
            Insert() : o_( fromjson( "{\"a\":\"b\"}" ) ) {}
            void doIt() const {
                client()->insert( ns(), o_ );
            }
            void check() const {
                ASSERT_EQUALS( 1, count() );
                checkOne( o_ );
            }
            void reset() const {
                deleteAll( ns() );
            }
        protected:
            BSONObj o_;
        };

        class InsertWithId : public Insert {
        public:
            InsertWithId() {
                o_ = fromjson( "{\"_id\":ObjectId(\"0f0f0f0f0f0f0f0f0f0f0f0f\"),\"a\":\"b\"}" );
            }
        };
        
        class InsertTwo : public Base {
        public:
            InsertTwo() : 
            o_( fromjson( "{\"a\":\"b\"}" ) ),
            t_( fromjson( "{\"c\":\"d\"}" ) ) {}
            void doIt() const {
                vector< BSONObj > v;
                v.push_back( o_ );
                v.push_back( t_ );
                client()->insert( ns(), v );
            }
            void check() const {
                ASSERT_EQUALS( 2, count() );
                checkOne( o_ );
                checkOne( t_ );
            }
            void reset() const {
                deleteAll( ns() );
            }
        private:
            BSONObj o_;
            BSONObj t_;
        };

        class InsertTwoIdentical : public Base {
        public:
            InsertTwoIdentical() : o_( fromjson( "{\"a\":\"b\"}" ) ) {}
            void doIt() const {
                client()->insert( ns(), o_ );
                client()->insert( ns(), o_ );
            }
            void check() const {
                ASSERT_EQUALS( 2, count() );
                checkAll( o_ );
            }
            void reset() const {
                deleteAll( ns() );
            }
        private:
            BSONObj o_;            
        };

        // TODO Maybe make this a test suite?
        class UpdateBase {
        public:
            void run() {
                runOne< Update >();
                runOne< UpsertInsert >();
                runOne< UpsertNoInsert >();
            }
        protected:
            UpdateBase( const BSONObj &q, const BSONObj &u ) : q_( q ), u_( u ) {} 
        private:
            template< class T >
            void runOne() const {
                T test( q_, u_ );
                test.run();
            }
            class Update : public Base {
            public:
                Update( const BSONObj &q, const BSONObj &u ) : q_( q ), u_( u ) {}
            protected:
                void doIt() const {
                    client()->update( ns(), q_, u_ );
                }
                void check() const {
                    ASSERT_EQUALS( 1, count() );
                }
                void reset() const {
                    deleteAll( ns() );
                    insert( q_ );                    
                }
                BSONObj q_, u_;
            };
            class UpsertInsert : public Update {
            public:
                UpsertInsert( const BSONObj &q, const BSONObj &u ) :
                Update( q, u ) {}
                void doIt() const {
                    client()->update( ns(), q_, u_, true );
                }
                void reset() const {
                    deleteAll( ns() );
                }
            };
            class UpsertNoInsert : public Update {
            public:
                UpsertNoInsert( const BSONObj &q, const BSONObj &u ) :
                Update( q, u ) {}
                void doIt() const {
                    client()->update( ns(), q_, u_, true );
                }                
            };
            BSONObj q_, u_;
        };

        class UpdateSameField : public UpdateBase {
        public:
            UpdateSameField() :
            UpdateBase( fromjson( "{\"a\":\"b\"}" ),
                       fromjson( "{\"a\":\"c\"}" ) ) {}
        };

        class UpdateDifferentField : public UpdateBase {
        public:
            UpdateDifferentField() :
            UpdateBase( fromjson( "{\"a\":\"b\"}" ),
                       fromjson( "{\"a\":\"b\",\"x\":\"y\"}" ) ) {}            
        };
                
    } // namespace Idempotence
    
    class All : public UnitTest::Suite {
    public:
        All() {
            add< LogBasic >();
            add< Idempotence::Insert >();
            add< Idempotence::InsertWithId >();
            add< Idempotence::InsertTwo >();
            // FIXME Decide what is correct & uncomment
//            add< Idempotence::InsertTwoIdentical >();
            // FIXME Decide what is correct & uncomment
//            add< Idempotence::UpdateSameField >();
            add< Idempotence::UpdateDifferentField >();
        }
    };
    
} // namespace ReplTests

UnitTest::TestPtr replTests() {
    return UnitTest::createSuite< ReplTests::All >();
}