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