@@ -2905,10 +2905,41 @@ TEST(watcher_fallback_still_detects) {
29052905 PASS ();
29062906}
29072907
2908+ /* Reindex counts for watcher_poll_only_watched_projects, split by project:
2909+ * the watched one, and anything else. */
2910+ static int ow_watched_calls = 0 ;
2911+ static int ow_other_calls = 0 ;
2912+ static int ow_index_callback (const char * name , const char * path , void * ud ) {
2913+ (void )path ;
2914+ (void )ud ;
2915+ if (name && strcmp (name , "projA-ow" ) == 0 ) {
2916+ ow_watched_calls ++ ;
2917+ } else {
2918+ ow_other_calls ++ ;
2919+ }
2920+ return 0 ;
2921+ }
2922+
29082923TEST (watcher_poll_only_watched_projects ) {
2909- /* Port of TestPollAllOnlyWatched:
2910- * Two repos exist, only one is watched → only the watched one
2911- * gets polled and can trigger reindex. */
2924+ /* Port of TestPollAllOnlyWatched (#49: the watcher polls its explicit
2925+ * watch list, never every project the store knows). Two repos exist and
2926+ * BOTH are registered in the store, as in the Go original; only A is
2927+ * watched. Contract: B never gets watcher state and is never reindexed,
2928+ * and A's change is reindexed.
2929+ *
2930+ * No verdict depends on one git probe succeeding. The earlier version
2931+ * asserted that ONE poll after the change reindexed exactly once, and it
2932+ * never registered B anywhere the watcher could see, so its B half could
2933+ * not fail. A poll whose git probe fails transiently (spawn refused under
2934+ * runner load) is a fail-closed no-op by design (#937: the baseline stays
2935+ * uncommitted and the next poll retries), so that assert read
2936+ * `index_call_count == 0, expected 1` on a loaded macOS runner. Holding
2937+ * one exhausted spawn with cbm_subprocess_force_spawn_eagain_for_testing
2938+ * at that poll reproduces the CI line exactly. The test now polls until
2939+ * A's reindex has happened, and after every poll asserts the states that
2940+ * hold whatever git answers: B has no watcher state and no reindex. A
2941+ * watcher that never reindexes A hangs here, which the runner reports as
2942+ * a failure, never a pass. */
29122943 char tmpdirA [256 ];
29132944 snprintf (tmpdirA , sizeof (tmpdirA ), "/tmp/cbm_watcher_owA_XXXXXX" );
29142945 char tmpdirB [256 ];
@@ -2941,19 +2972,32 @@ TEST(watcher_poll_only_watched_projects) {
29412972 wt_git (tmpdirB , "add b.txt" );
29422973 wt_git (tmpdirB , "commit -q -m init" );
29432974
2975+ /* Both projects are known to the store. A watcher that polled stored
2976+ * projects instead of its watch list (the pre-#49 scan-all) adopts B. */
29442977 cbm_store_t * store = cbm_store_open_memory ();
2945- cbm_watcher_t * w = cbm_watcher_new (store , index_callback , NULL );
2978+ ASSERT_EQ (cbm_store_upsert_project (store , "projA-ow" , tmpdirA ), CBM_STORE_OK );
2979+ ASSERT_EQ (cbm_store_upsert_project (store , "projB-ow" , tmpdirB ), CBM_STORE_OK );
2980+ cbm_watcher_t * w = cbm_watcher_new (store , ow_index_callback , NULL );
29462981
29472982 /* Only watch A — B is NOT watched */
29482983 cbm_watcher_watch (w , "projA-ow" , tmpdirA );
29492984 ASSERT_EQ (cbm_watcher_watch_count (w ), 1 );
2950- index_call_count = 0 ;
2985+ ow_watched_calls = 0 ;
2986+ ow_other_calls = 0 ;
29512987
2952- /* Baseline */
2953- cbm_watcher_poll_once (w );
2954- ASSERT_EQ (index_call_count , 0 );
2988+ /* Baseline poll. Its git outcome is not asserted: if a probe fails here,
2989+ * a later poll takes the baseline instead. A baseline poll never indexes,
2990+ * whatever git answers, and it must not adopt B. */
2991+ ASSERT_EQ (cbm_watcher_poll_once (w ), 0 );
2992+ ASSERT_EQ (ow_watched_calls + ow_other_calls , 0 );
2993+ ASSERT_EQ (cbm_watcher_index_failure_count (w , "projB-ow" ), -1 );
2994+ ASSERT_EQ (cbm_watcher_watch_count (w ), 1 );
29552995
2956- /* Make BOTH repos dirty */
2996+ /* Make BOTH repos dirty. A dirty worktree, not a commit: the baseline
2997+ * keeps the dirty signature at "clean known", so A's change is seen
2998+ * whenever the baseline lands, even if the baseline poll above failed. A
2999+ * commit made before a late baseline would be adopted as the baseline
3000+ * HEAD and never reindexed. */
29573001 {
29583002 char _p [1024 ];
29593003 snprintf (_p , sizeof (_p ), "%s/a.txt" , tmpdirA );
@@ -2965,10 +3009,18 @@ TEST(watcher_poll_only_watched_projects) {
29653009 th_append_file (_p , "dirty\n" );
29663010 }
29673011
2968- /* Poll — only A should trigger (B is not watched) */
2969- cbm_watcher_touch (w , "projA-ow" );
2970- cbm_watcher_poll_once (w );
2971- ASSERT_EQ (index_call_count , 1 );
3012+ /* Poll until A's reindex. touch clears the adaptive interval so every
3013+ * poll checks A; each poll runs at most one attempt for A, so the loop
3014+ * stops at exactly one. After EVERY poll, B has no watcher state and no
3015+ * reindex, whether or not that poll's git probes succeeded. */
3016+ while (ow_watched_calls == 0 ) {
3017+ cbm_watcher_touch (w , "projA-ow" );
3018+ cbm_watcher_poll_once (w );
3019+ ASSERT_EQ (ow_other_calls , 0 );
3020+ ASSERT_EQ (cbm_watcher_index_failure_count (w , "projB-ow" ), -1 );
3021+ ASSERT_EQ (cbm_watcher_watch_count (w ), 1 );
3022+ }
3023+ ASSERT_EQ (ow_watched_calls , 1 );
29723024
29733025 cbm_watcher_free (w );
29743026 cbm_store_close (store );
0 commit comments