--- /dev/null
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 890B9431FBD\r
+ for <notmuch@notmuchmail.org>; Sun, 23 Mar 2014 14:01:25 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id umAYWFbDynLk for <notmuch@notmuchmail.org>;\r
+ Sun, 23 Mar 2014 14:01:19 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-6.mit.edu (dmz-mailsec-scanner-6.mit.edu\r
+ [18.7.68.35])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id C2642431FB6\r
+ for <notmuch@notmuchmail.org>; Sun, 23 Mar 2014 14:01:18 -0700 (PDT)\r
+X-AuditID: 12074423-f79476d000000c51-28-532f4b9df6c2\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 85.13.03153.D9B4F235; Sun, 23 Mar 2014 17:01:17 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id s2NL1FlT028087; \r
+ Sun, 23 Mar 2014 17:01:15 -0400\r
+Received: from drake.dyndns.org\r
+ (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+ [216.15.114.40]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s2NL1Dt9010614\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Sun, 23 Mar 2014 17:01:15 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1WRpW0-0002Zy-58; Sun, 23 Mar 2014 17:01:08 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] test: Test thread linking in all possible delivery orders\r
+Date: Sun, 23 Mar 2014 17:00:56 -0400\r
+Message-Id: <1395608456-9673-1-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.8.4.rc3\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFprJIsWRmVeSWpSXmKPExsUixG6nojvXWz/YYN8fA4vrN2cyOzB6PFt1\r
+ izmAMYrLJiU1J7MstUjfLoEr4/SnJ0wFb4Urll/6zd7AuJ6/i5GTQ0LAROJmQy87hC0mceHe\r
+ ejYQW0hgNpPEypXRXYxcQPZGRok1HVcYIRJ3mCSuTFSCSMxllLhyo50FJMEmoCGxbf9ysCIR\r
+ AWmJnXdns3YxcnAwC6hJ/OlSAQkLC3hJHF7YBbaMRUBVYtr1DmYQm1fAXuLVqWZGiCOUJBae\r
+ 2sY6gZF3ASPDKkbZlNwq3dzEzJzi1GTd4uTEvLzUIl0zvdzMEr3UlNJNjOAQcFHewfjnoNIh\r
+ RgEORiUe3hOfdIOFWBPLiitzDzFKcjApifL+dtcPFuJLyk+pzEgszogvKs1JLT7EKMHBrCTC\r
+ G+kKlONNSaysSi3Kh0lJc7AoifPKc2gHCwmkJ5akZqemFqQWwWRlODiUJHgzvIAaBYtS01Mr\r
+ 0jJzShDSTBycIMN5gIbP8wQZXlyQmFucmQ6RP8WoKCXOmwzSLACSyCjNg+uFxegrRnGgV4R5\r
+ J4FU8QDjG677FdBgJqDB4U16IINLEhFSUg2MF/ZouJ/Unc3MItMZPnV5EstWv9KNzGX9F3Tf\r
+ fm4I0P2zYSrT1F89O6/nll5t913Xz3MhQONDRrTq1x0fry5fOGliZef0FjGOvH4mrWvLNu49\r
+ neB/5uD902bfmfWXiQv+UFh/jX/Cc40V74rThYSe5PnyZx68yH1HiKX/YGHht+9CNlnfe54H\r
+ KbEUZyQaajEXFScCAFA3WyisAgAA\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 23 Mar 2014 21:01:25 -0000\r
+\r
+This test delivers all possible (single-root) four-message threads in\r
+all possible orders and checks that notmuch successfully links them\r
+into threads in every case.\r
+\r
+This is introduced as a new test (rather than just adding it to\r
+T050-new) because it's much easier for this to start with an empty\r
+database.\r
+---\r
+\r
+I thought I saw a bug in the thread linking code. It turned out to be\r
+okay, but I wrote this test to convince myself.\r
+\r
+As far as I can tell, we don't have any tests specifically for thread\r
+linking. We certainly don't have any that are this systematic.\r
+\r
+ test/T051-new-linking.sh | 43 +++++++++++++++++++++++++++++++++++++++++++\r
+ 1 file changed, 43 insertions(+)\r
+ create mode 100755 test/T051-new-linking.sh\r
+\r
+diff --git a/test/T051-new-linking.sh b/test/T051-new-linking.sh\r
+new file mode 100755\r
+index 0000000..b6d778a\r
+--- /dev/null\r
++++ b/test/T051-new-linking.sh\r
+@@ -0,0 +1,43 @@\r
++#!/usr/bin/env bash\r
++test_description='"notmuch new" thread linking'\r
++\r
++. ./test-lib.sh\r
++\r
++test_begin_subtest "All four-message threads get linked in all delivery orders"\r
++# Generate all possible single-root four message thread structures.\r
++# Each line in THREADS is a thread structure, where the n'th field is\r
++# the parent of message n.\r
++THREADS=$(python -c '\r
++def mkTrees(free, tree={}):\r
++ if not free:\r
++ print(" ".join(map(str, [msg[1] for msg in sorted(tree.items())])))\r
++ return\r
++ # Attach each free message to each message in the tree (if there is\r
++ # no tree, make the free message the root)\r
++ for msg in sorted(free):\r
++ parents = sorted(tree.keys()) if tree else ["none"]\r
++ for parent in parents:\r
++ ntree = tree.copy()\r
++ ntree[msg] = parent\r
++ mkTrees(free - set([msg]), ntree)\r
++mkTrees(set(range(4)))')\r
++for ((n = 0; n < 4; n++)); do\r
++ # Deliver the n'th message of every thread\r
++ thread=0\r
++ while read -a parents; do\r
++ parent=${parents[$n]}\r
++ generate_message \\r
++ [id]=m$n@t$thread [in-reply-to]="\<m$parent@t$thread\>" \\r
++ [subject]=p$thread [from]=m$n\r
++ thread=$((thread + 1))\r
++ done <<< "$THREADS"\r
++ notmuch new > /dev/null\r
++done\r
++output=$(notmuch search '*' | notmuch_search_sanitize)\r
++nthreads=$(wc -l <<< "$THREADS")\r
++expected=$(for ((i = 0; i < $nthreads; i++)); do\r
++ echo "thread:XXX 2001-01-05 [4/4] m3, m2, m1, m0; p$i (inbox unread)"\r
++ done)\r
++test_expect_equal "$output" "$expected"\r
++\r
++test_done\r
+-- \r
+1.8.4.rc3\r
+\r