File: bdb_multi_proc.test

package info (click to toggle)
db5.3 5.3.28%2Bdfsg2-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 158,620 kB
  • sloc: ansic: 448,573; java: 111,824; tcl: 80,544; sh: 44,264; cs: 33,697; cpp: 21,600; perl: 14,557; xml: 10,799; makefile: 4,028; javascript: 1,998; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (467 lines) | stat: -rw-r--r-- 11,879 bytes parent folder | download | duplicates (8)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for parallel multi-process 
# access to a database.
set sqldir [file dirname $argv0]
set testdir $sqldir/../../lang/sql/sqlite/test

source $testdir/tester.tcl
# Contains the definition of do_sync and do_multi_proc_test
source $sqldir/../../test/tcl_utils/multi_proc_utils.tcl
# Contains the definition of available_ports
source $sqldir/../../test/tcl_utils/common_test_utils.tcl
# Skip this test if threads are not enabled.  The do_sync function
# requires threads.
if {![run_thread_tests]} { 
    puts "Tcl built without threads enabled, skipping test."
    finish_test ; return 
}
if [catch {package require Thread}] {
    puts "Tcl does not contain the Thread library, skipping test."
    finish_test ; return 
}

# The first test tests that one process can read data inserted
# into the database by another process. 
set myports [ available_ports 2]
set myPort1 [ lindex $myports 0]
set myPort2 [ lindex $myports 1]
do_multi_proc_test bdb_multi_proc-1 [list {
    # Process 1 code
    set cmd_args [ lindex $argv 0 ]
    set myPort [ lindex $cmd_args 0 ]
    set clientPorts [lindex $cmd_args 1 ]
    set timeout 20

    # The scripts are run relative to the build_X directory 
    set testdir ../lang/sql/sqlite/test
    # For the definition of do_test
    source $testdir/tester.tcl
    source ../test/tcl_utils/multi_proc_utils.tcl
    sqlite3 db procs.db

    # Create the tables
    do_test bdb_multi_proc-1.1.1 {
	db eval { 
	    BEGIN;
	    CREATE TABLE t1(a);
	    COMMIT;
	}
    } {}

    # Wake up the other process
    do_test bdb_multi_proc-1.1.2 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Pause while the other process inserts into the table
    do_test bdb_multi_proc-1.1.3 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Read from the table
    do_test bdb_multi_proc-1.1.4 {
	db eval { 
	    SELECT * from t1;
	}
    } {1}

    # Wake up the other process
    do_test bdb_multi_proc-1.1.5 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    db close
    finish_test
} {
    # Process 2 code.
    set cmd_args [ lindex $argv 0 ]
    set myPort [ lindex $cmd_args 0 ]
    set clientPorts [lindex $cmd_args 1 ]
    set timeout 20

    # The scripts are run relative to the build_X directory 
    set testdir ../lang/sql/sqlite/test
    # For the definition of do_test
    source $testdir/tester.tcl
    source ../test/tcl_utils/multi_proc_utils.tcl
    sqlite3 db procs.db

    # Wait while the other process creates the table
    do_test bdb_multi_proc-1.2.1 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Insert into the table
    do_test bdb_multi_proc-1.2.2 {
	db eval { 
	    INSERT INTO t1 values(1);
	}
    } {}

    # Wake up the other process
    do_test bdb_multi_proc-1.2.3 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Wait while the other process reads the table
    do_test bdb_multi_proc-1.2.4 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    db close
    finish_test

# Below is the argument list for the processes.  The first list is
# passed to Process 1, and the second list is passed to Process 2.
# The lists consist of:
# first myPort - Port for the server of the current process
# last myPort - Port for the server of the other process
}] [list [list $myPort1 $myPort2] \
	[list $myPort2 $myPort1]]

catch {file delete -force -- procs.db}
catch {file delete -force -- procs.db-journal}

# The second test tests that three processes can write data to the 
# database and read each other's work.
set myports [ available_ports 3]
set myPort1 [ lindex $myports 0]
set myPort2 [ lindex $myports 1]
set myPort3 [ lindex $myports 2]
do_multi_proc_test bdb_multi_proc-2 [list {
    # Process 1
    set cmd_args [ lindex $argv 0 ]
    set myPort [ lindex $cmd_args 0 ]
    set clientPorts {}
    lappend clientPorts [lindex $cmd_args 1 ]
    lappend clientPorts [lindex $cmd_args 2 ]
    set timeout 20

    # The scripts are run relative to the build_X directory  
    set testdir ../lang/sql/sqlite/test
    # For the definition of do_test
    source $testdir/tester.tcl
    source ../test/tcl_utils/multi_proc_utils.tcl
    sqlite3 db procs.db

    # Create the tables
    do_test bdb_multi_proc-2.1.1 {
	db eval { 
	    BEGIN;
	    CREATE TABLE t1(a);
	    COMMIT;
	}
    } {}

    # Wake up the other proceses
    do_test bdb_multi_proc-2.1.2 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Pause while process 2 inserts into the table
    do_test bdb_multi_proc-2.1.3 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Read from the table
    do_test bdb_multi_proc-2.1.4 {
	db eval { 
	    SELECT * from t1;
	}
    } {2}

    # Wake up the other processes after verifying process 2 write
    do_test bdb_multi_proc-2.1.5 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Pause while process 3 writes to the table
    do_test bdb_multi_proc-2.1.5 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Read from the table
    do_test bdb_multi_proc-2.1.6 {
	db eval { 
	    SELECT * from t1;
	}
    } {2 3}

    db close
    finish_test
} {
    # Process 2
    set cmd_args [ lindex $argv 0 ]
    set myPort [ lindex $cmd_args 0 ]
    set clientPorts {}
    lappend clientPorts [lindex $cmd_args 1 ]
    lappend clientPorts [lindex $cmd_args 2 ]
    set timeout 20

    # The scripts are run relative to the build_X directory 
    set testdir ../lang/sql/sqlite/test
    # For the definition of do_test
    source $testdir/tester.tcl
    source ../test/tcl_utils/multi_proc_utils.tcl
    sqlite3 db procs.db

    # Wait while process 1 creates the table
    do_test bdb_multi_proc-2.2.1 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Read from the table
    do_test bdb_multi_proc-2.2.2 {
	db eval { 
	    SELECT * from t1;
	}
    } {}

    # Insert into the table
    do_test bdb_multi_proc-2.2.3 {
	db eval { 
	    INSERT INTO t1 values(2);
	}
    } {}

    # Wake up the other processes
    do_test bdb_multi_proc-2.2.4 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Wait while process 1 verifies our write
    do_test bdb_multi_proc-2.2.5 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Wait while process 3 inserts into the table
    do_test bdb_multi_proc-2.2.5 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Read from the table
    do_test bdb_multi_proc-2.2.6 {
	db eval { 
	    SELECT * from t1;
	}
    } {2 3}

    db close
    finish_test
} {
    # Process 3
    set cmd_args [ lindex $argv 0 ]
    set myPort [ lindex $cmd_args 0 ]
    set clientPorts {}
    lappend clientPorts [lindex $cmd_args 1 ]
    lappend clientPorts [lindex $cmd_args 2 ]
    set timeout 20
    # The scripts are run relative to the build_X directory 
    set testdir ../lang/sql/sqlite/test
    # For the definition of do_test
    source $testdir/tester.tcl
    source ../test/tcl_utils/multi_proc_utils.tcl
    sqlite3 db procs.db

    # Wait while process 1 creates the table
    do_test bdb_multi_proc-2.3.1 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Wait while process 2 inserts into the table
    do_test bdb_multi_proc-2.3.2 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Wait while process 1 verifies the write from process 2
    do_test bdb_multi_proc-2.3.2 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Read from the table
    do_test bdb_multi_proc-2.3.3 {
	db eval { 
	    SELECT * from t1;
	}
    } {2}

    # Insert into the table
    do_test bdb_multi_proc-2.3.4 {
	db eval { 
	    INSERT INTO t1 values(3);
	}
    } {}

    # Wake up the other processes
    do_test bdb_multi_proc-2.3.5 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    db close
    finish_test
# Below is the argument lists for the processes, consisting of
# first myPort - Port for the server of the current process
# last two myPort - Ports for the servers of the other processes
}] [list [list $myPort1 $myPort2 $myPort3] \
	[list $myPort2 $myPort1 $myPort3] \
	[list $myPort3 $myPort1 $myPort2]]

catch {file delete -force -- procs.db}
catch {file delete -force -- procs.db-journal}

sqlite3 db procs2.db

do_test bdb_multi_proc-3.0 {
    db eval { CREATE TABLE atable(a); }
} {}

db close

# Check for a bug that could cause deadlock between
# two processes that create new tables SR #20722
set myports [ available_ports 2]
set myPort1 [ lindex $myports 0]
set myPort2 [ lindex $myports 1]
do_multi_proc_test bdb_multi_proc-3 [list {
    # Process 1 code
    set cmd_args [ lindex $argv 0 ]
    set myPort [ lindex $cmd_args 0 ]
    set clientPorts [lindex $cmd_args 1 ]
    set timeout 20

    set testdir ../lang/sql/sqlite/test
    source $testdir/tester.tcl
    source ../test/tcl_utils/multi_proc_utils.tcl
    sqlite3 db procs2.db

    # Create the table 1
    do_test bdb_multi_proc-3.1.1 {
	db eval { 
	    CREATE TABLE t1(a);
	}
    } {}

    # Wait on process 2
    do_test bdb_multi_proc-3.1.2 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Start the transaction
    do_test bdb_multi_proc-3.1.3 {
	db eval { 
	    BEGIN IMMEDIATE;
	}
    } {}

    # Wait on process 2
    do_test bdb_multi_proc-3.1.4 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Insert into the table we created
    do_test bdb_multi_proc-3.1.5 {
	db eval { 
	    INSERT INTO t1 VALUES(1);
	}
    } {}

    # Wake up process 2
    do_test bdb_multi_proc-3.1.6 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Let process 2 become blocked
    after 3000

    # Insert into the table Process 2 created
    do_test bdb_multi_proc-3.1.7 {
	db eval { 
	    INSERT INTO t2 VALUES(2);
	}
    } {}

    # End the transaction letting process 2 continue
    do_test bdb_multi_proc-3.1.8 {
	db eval { 
	    COMMIT;
	}
    } {}

    db close
    finish_test
} {
    # Process 2 code.
    set cmd_args [ lindex $argv 0 ]
    set myPort [ lindex $cmd_args 0 ]
    set clientPorts [lindex $cmd_args 1 ]
    set timeout 5

    set testdir ../lang/sql/sqlite/test
    source $testdir/tester.tcl
    source ../test/tcl_utils/multi_proc_utils.tcl
    sqlite3 db procs2.db

    # Create the table2
    do_test bdb_multi_proc-3.2.1 {
	db eval { 
	    CREATE TABLE t2(a);
	}
    } {}

    # Wait on process 1
    do_test bdb_multi_proc-3.2.2 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Start the transaction
    do_test bdb_multi_proc-3.2.3 {
	db eval { 
	    BEGIN IMMEDIATE;
	}
    } {}

    # Wait on process 1
    do_test bdb_multi_proc-3.2.4 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Let process 1 insert first
    do_test bdb_multi_proc-3.2.5 {
	set ret [do_sync $myPort $clientPorts $timeout]
    } {0}

    # Insert into the table we created, will become
    # blocked here until process 1 commits
    do_test bdb_multi_proc-3.2.6 {
	db eval { 
	    INSERT INTO t2 VALUES(2);
	}
    } {}

    # End the transaction
    do_test bdb_multi_proc-3.2.8 {
	db eval { 
	    COMMIT;
	}
    } {}

    db close
    finish_test

# Below is the argument list for the processes.  The first list is
# passed to Process 1, and the second list is passed to Process 2.
# The lists consist of:
# first myPort - Port for the server of the current process
# last myPort - Port for the server of the other process
}] [list [list $myPort1 $myPort2] \
	[list $myPort2 $myPort1]]

catch {file delete -force -- procs2.db}
catch {file delete -force -- procs2.db-journal}

finish_test