-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pyreplica.py
217 lines (195 loc) · 7.97 KB
/
test_pyreplica.py
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
#!/usr/bin/env python
# simple test cases for pyreplica
#
# Copyright (C) 2008 Mariano Reingart <mariano@nsis.com.ar>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
import datetime
import pyreplica
import psycopg2
import unittest
DSN = "dbname='%s' user='postgres' port=5432 host=localhost"
DSN0 = DSN % "master"
DSN1 = DSN % "slave"
DEBUG = False
class PyReplicaTests(unittest.TestCase):
test_table = "table1"
def setUp(self):
# connect to postgres
con = psycopg2.connect(DSN % "postgres")
cur = con.cursor()
# create both test databases
con.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
try:
cur.execute("DROP DATABASE master")
cur.execute("DROP DATABASE slave")
except:
pass
cur.execute("CREATE DATABASE master")
cur.execute("CREATE DATABASE slave")
# connect to master/slave
self.con0, self.con1 = pyreplica.connect(DSN0,DSN1, self.debug)
self.cur0 = self.con0.cursor()
self.cur1 = self.con1.cursor()
# create test table
sql = """CREATE TABLE %s (
id SERIAL PRIMARY KEY,
t TEXT,
d TIMESTAMP DEFAULT now(),
f FLOAT8 DEFAULT random(),
n NUMERIC(100) DEFAULT random(),
ba BYTEA
)""" % self.test_table
self.cur0.execute(sql)
self.cur1.execute(sql)
# install pyreplica
self.cur0.execute(open("master-install.sql").read())
self.con0.commit()
#self.cur1.execute(open("slave-install.sql").read())
self.con1.commit()
def tearDown(self):
self.con0.rollback()
self.con1.rollback()
self.con0.close()
self.con1.close()
con = psycopg2.connect(DSN % "postgres")
con.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
# drop both test databases
cur.execute("DROP DATABASE master")
cur.execute("DROP DATABASE slave")
def insert(self,cur,commit=True,**kwargs):
cur.execute("INSERT INTO %s (%s) VALUES (%s)" % (self.test_table,
', '.join(["%s" % k for k in kwargs.keys()]),
', '.join(["%%(%s)s" % k for k in kwargs.keys()])), kwargs)
if DEBUG: print cur.query
cur.execute("SELECT * FROM %s WHERE id = currval('%s_id_seq')" %
(self.test_table,self.test_table) )
if commit: cur.connection.commit()
return cur.fetchone()[0]
def update(self,cur,id,**kwargs):
fields = kwargs.keys()
kwargs['id'] = id
cur.execute("UPDATE %s SET %s WHERE id=%%(id)s" % (self.test_table,
', '.join(["%s=%%(%s)s" % (k,k) for k in fields])), kwargs)
if DEBUG: print cur.query
cur.connection.commit()
def delete(self,cur,id):
cur.execute("DELETE FROM %s WHERE id=%%s" % self.test_table, (id,))
cur.connection.commit()
def rowcount(self, cur):
"return the rowcount from test table"
cur.execute("SELECT COUNT(*) FROM %s " % self.test_table)
return cur.fetchone()[0]
def diff(self,phase):
"compare tables in master and slave"
self.cur0.execute("SELECT * FROM %s ORDER BY id" % self.test_table)
self.cur1.execute("SELECT * FROM %s ORDER BY id" % self.test_table)
self.assertTrue(self.cur0.rowcount==self.cur1.rowcount,
"%s: row count diffiers: %d != %d" %
(phase,self.cur0.rowcount,self.cur1.rowcount))
for i in range(self.cur0.rowcount):
row0 = self.cur0.fetchone()
row1 = self.cur1.fetchone()
#self.assertTrue(id0==id1, "%s: id %s != %s" % (phase,id0,id1))
self.assertTrue(row0==row1, "%s: %s != %s" % (phase,row0,row1))
def debug(self,message,level=1):
if not level: # conflict warning?
# we can be inside a TPC transaction, store warning and continue
self.warnings.append(message)
if DEBUG: print message
def replicate(self, must_conflict = False):
self.warnings = []
pyreplica.replicate(self.cur0, self.cur1, debug=self.debug)
# check for warning messages
if not must_conflict:
self.assertFalse(self.warnings,
"There were unexpected conflict warnings:\n%s" %
'\n'.join(self.warnings))
else:
self.assertTrue(self.warnings,
"There were no conflict but they were expected")
def test_basic(self):
"Test normal insert, update, delete"
id = self.insert(self.cur0,t='spam')
self.replicate()
self.diff("INSERT")
self.update(self.cur0,id,t='eggs',f=3.14,d=datetime.datetime.now())
self.replicate()
self.diff("UPDATE")
self.delete(self.cur0,id)
self.replicate()
self.diff("DELETE")
id = self.insert(self.cur0,t='spam')
self.replicate()
def test_conflicts(self):
"Test normal insert, update, delete with conflicts"
id = self.insert(self.cur0,t='spam')
self.replicate()
self.diff("INSERT")
self.update(self.cur1,id,t='bacon',f=2.0,d=datetime.datetime.now())
self.update(self.cur0,id,t='eggs',f=None,d=None)
self.replicate(must_conflict=True)
self.diff("INSERT CONFLICT")
self.delete(self.cur1,id) # delete on slave so it must conflict
self.delete(self.cur0,id)
self.replicate(must_conflict=True)
self.diff("DELETE CONFLICT")
def test_integrity(self):
"Test integrity error"
id = self.insert(self.cur0,t='spam')
id = self.insert(self.cur1,t='spam')
self.assertRaises(psycopg2.IntegrityError,self.replicate)
self.con0.tpc_rollback()
self.con1.tpc_rollback()
def test_multiple(self):
"Test multiple (bulk) insert, update and deletes "
for x in xrange(1000):
i = self.insert(self.cur0,t='spam')
self.replicate()
self.diff("INSERT MULTIPLE")
self.cur0.execute("UPDATE %s SET t=%%s" % self.test_table, ("bacon",))
self.cur0.connection.commit()
self.replicate()
self.diff("UPDATE MULTIPLE")
self.cur0.execute("DELETE FROM %s" % self.test_table, ())
self.cur0.connection.commit()
self.replicate()
self.diff("DELETE MULTIPLE")
def test_bytea(self):
"Test bytea datatype"
self.insert(self.cur0,
ba=(psycopg2.Binary(''.join([chr(i) for i in xrange(0,255)])),))
self.replicate()
self.diff("BYTEA INSERT")
def test_long_transaction(self):
"Test long/overlapping transactions"
# open new connection (to do parallel transactions)
con0, con1 = pyreplica.connect(DSN0,DSN1, self.debug)
cur0 = con0.cursor()
# insert, but keep the transaction open
i = self.insert(cur0, commit=False, t='spam')
# insert and commit transaction
i = self.insert(self.cur0,commit=True, t='eggs')
self.replicate()
self.diff("INSERT LONG 2")
self.assertTrue(self.rowcount(self.cur1)==1,"It showld be one rows")
con0.commit()
# clean up
con0.close()
con1.close()
self.replicate()
self.diff("INSERT LONG 1")
self.assertTrue(self.rowcount(self.cur1)==2,"It showld be two rows")
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(PyReplicaTests)
#suite.debug()
unittest.TextTestRunner(verbosity=2).run(suite)