]> git.notmuchmail.org Git - notmuch/blob - test/atomicity
new: Wrap adding and removing messages in atomic sections.
[notmuch] / test / atomicity
1 #!/bin/bash
2 test_description='atomicity'
3 . ./test-lib.sh
4
5 # This script tests the effects of killing and restarting "notmuch
6 # new" at arbitrary points.  If notmuch new is properly atomic, the
7 # final database contents should be the same regardless of when (or
8 # if) it is killed and restarted.
9
10 if test_expect_success "prereq: GDB is present" "which gdb"; then
11     test_set_prereq GDB
12 fi
13
14 # Create a maildir structure to also stress flag synchronization
15 mkdir $MAIL_DIR/cur
16 mkdir $MAIL_DIR/new
17 mkdir $MAIL_DIR/tmp
18 mkdir $MAIL_DIR/.remove-dir
19
20 # Prepare the initial database
21 generate_message [subject]='Duplicate' [filename]='duplicate:2,' [dir]=cur
22 generate_message [subject]='Remove' [filename]='remove:2,' [dir]=cur
23 generate_message [subject]='"Remove duplicate"' [filename]='remove-duplicate:2,' [dir]=cur
24 cp $MAIL_DIR/cur/remove-duplicate:2, $MAIL_DIR/cur/remove-duplicate-copy:2,
25 generate_message [subject]='Rename' [filename]='rename:2,' [dir]=cur
26 generate_message [subject]='"Rename duplicate"' [filename]='rename-duplicate:2,' [dir]=cur
27 generate_message [subject]='"Move 1"' [filename]='move1:2,' [dir]=cur
28 generate_message [subject]='"Move 2"' [filename]='move2:2,' [dir]=new
29 generate_message [subject]='Flag' [filename]='flag:2,' [dir]=cur
30 generate_message [subject]='"Flag duplicate"' [filename]='flag-duplicate:2,' [dir]=cur
31 cp $MAIL_DIR/cur/flag-duplicate:2, $MAIL_DIR/cur/flag-duplicate-copy:2,F
32 generate_message [subject]='"Remove directory"' [filename]='remove-directory:2,' [dir]=.remove-dir
33 generate_message [subject]='"Remove directory duplicate"' [filename]='remove-directory-duplicate:2,' [dir]=.remove-dir
34 cp $MAIL_DIR/.remove-dir/remove-directory-duplicate:2, $MAIL_DIR/cur/
35 notmuch new > /dev/null
36
37 # Make all maildir changes, but *don't* update the database
38 generate_message [subject]='Added' [filename]='added:2,' [dir]=cur
39 cp $MAIL_DIR/cur/duplicate:2, $MAIL_DIR/cur/duplicate-copy:2,
40 generate_message [subject]='"Add duplicate"' [filename]='add-duplicate:2,' [dir]=cur
41 generate_message [subject]='"Add duplicate copy"' [filename]='add-duplicate-copy:2,' [dir]=cur
42 rm $MAIL_DIR/cur/remove:2,
43 rm $MAIL_DIR/cur/remove-duplicate-copy:2,
44 mv $MAIL_DIR/cur/rename:2, $MAIL_DIR/cur/renamed:2,
45 mv $MAIL_DIR/cur/rename-duplicate:2, $MAIL_DIR/cur/renamed-duplicate:2,
46 mv $MAIL_DIR/cur/move1:2, $MAIL_DIR/new/move1:2,
47 mv $MAIL_DIR/new/move2:2, $MAIL_DIR/cur/move2:2,
48 mv $MAIL_DIR/cur/flag:2, $MAIL_DIR/cur/flag:2,F
49 rm $MAIL_DIR/cur/flag-duplicate-copy:2,F
50 rm $MAIL_DIR/.remove-dir/remove-directory:2,
51 rm $MAIL_DIR/.remove-dir/remove-directory-duplicate:2,
52 rmdir $MAIL_DIR/.remove-dir
53
54 # Prepare a snapshot of the updated maildir.  The gdb script will
55 # update the database in this snapshot as it goes.
56 cp -ra $MAIL_DIR $MAIL_DIR.snap
57 cp ${NOTMUCH_CONFIG} ${NOTMUCH_CONFIG}.snap
58 NOTMUCH_CONFIG=${NOTMUCH_CONFIG}.snap notmuch config set database.path $MAIL_DIR.snap
59
60
61
62 # Execute notmuch new and, at every call to rename, snapshot the
63 # database, run notmuch new again on the snapshot, and capture the
64 # results of search.
65 #
66 # -tty /dev/null works around a conflict between the 'timeout' wrapper
67 # and gdb's attempt to control the TTY.
68 export MAIL_DIR
69 gdb -tty /dev/null -batch -x $TEST_DIRECTORY/atomicity.gdb notmuch >/dev/null 2>/dev/null
70
71 # Get the final, golden output
72 notmuch search '*' > expected
73
74 # Check output against golden output
75 outcount=$(cat outcount)
76 echo -n > searchall
77 echo -n > expectall
78 for ((i = 0; i < $outcount; i++)); do
79     if ! cmp -s search.$i expected; then
80         # Find the range of interruptions that match this output
81         for ((end = $i + 1 ; end < $outcount; end++)); do
82             if ! cmp -s search.$i search.$end; then
83                 break
84             fi
85         done
86         echo "When interrupted after $test/backtrace.$(expr $i - 1) (abort points $i-$(expr $end - 1))" >> searchall
87         cat search.$i >> searchall
88         cat expected >> expectall
89         echo >> searchall
90         echo >> expectall
91
92         i=$(expr $end - 1)
93     fi
94 done
95
96 test_begin_subtest '"notmuch new" is idempotent under arbitrary aborts'
97 test_expect_equal_file GDB searchall expectall
98
99 test_expect_success GDB "detected $outcount>10 abort points" "test $outcount -gt 10"
100
101 test_done