@chongdashu/cc-statusline 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,67 +1,67 @@
1
- #!/bin/bash
2
- # Test script for ccusage process locking mechanism
3
- #
4
- # This script simulates the file-based locking behavior implemented in the
5
- # ccusage integration to prevent concurrent process spawning.
6
- #
7
- # Usage: echo '{}' | ./test/test-statusline-with-lock.sh
8
- #
9
- # To test concurrent execution:
10
- # for i in {1..10}; do echo '{}' | ./test/test-statusline-with-lock.sh & done
11
- #
12
- # Expected behavior:
13
- # - Only one process should run ccusage at a time
14
- # - Other processes should skip execution gracefully
15
- # - No process pile-up or resource leaks should occur
16
-
17
- set -euo pipefail
18
-
19
- # Mock JSON input handling
20
- json_input=$(cat)
21
-
22
- # Implement file-based locking to prevent concurrent executions
23
- LOCK_FILE="/tmp/ccusage_statusline.lock"
24
- LOCK_PID_FILE="/tmp/ccusage_statusline.pid"
25
-
26
- # Function to check if process is still running
27
- is_process_running() {
28
- local pid=$1
29
- if [ -z "$pid" ]; then return 1; fi
30
- kill -0 "$pid" 2>/dev/null
31
- }
32
-
33
- # Try to acquire lock
34
- if mkdir "$LOCK_FILE" 2>/dev/null; then
35
- # Lock acquired, save our PID
36
- echo $$ > "$LOCK_PID_FILE"
37
-
38
- # Run ccusage with timeout (simulated with echo for testing)
39
- echo "[$$] Running ccusage..." >&2
40
-
41
- # Simulate ccusage work
42
- sleep 0.5
43
-
44
- # Generate mock output
45
- echo "📁 ~/test 🌿 main 🤖 Claude [simulated from $$]"
46
-
47
- # Clean up lock
48
- rm -f "$LOCK_PID_FILE" 2>/dev/null
49
- rmdir "$LOCK_FILE" 2>/dev/null
50
- else
51
- # Lock exists, check if it's stale
52
- if [ -f "$LOCK_PID_FILE" ]; then
53
- old_pid=$(cat "$LOCK_PID_FILE" 2>/dev/null)
54
- if ! is_process_running "$old_pid"; then
55
- # Stale lock, clean it up
56
- echo "[$$] Cleaning stale lock from $old_pid" >&2
57
- rm -f "$LOCK_PID_FILE" 2>/dev/null
58
- rmdir "$LOCK_FILE" 2>/dev/null
59
- else
60
- echo "[$$] Skipped - lock held by $old_pid" >&2
61
- fi
62
- else
63
- echo "[$$] Skipped - lock exists" >&2
64
- fi
65
- # Return cached/default output when skipped
66
- echo "📁 ~/test 🌿 main 🤖 Claude [cached]"
67
- fi
1
+ #!/bin/bash
2
+ # Test script for ccusage process locking mechanism
3
+ #
4
+ # This script simulates the file-based locking behavior implemented in the
5
+ # ccusage integration to prevent concurrent process spawning.
6
+ #
7
+ # Usage: echo '{}' | ./test/test-statusline-with-lock.sh
8
+ #
9
+ # To test concurrent execution:
10
+ # for i in {1..10}; do echo '{}' | ./test/test-statusline-with-lock.sh & done
11
+ #
12
+ # Expected behavior:
13
+ # - Only one process should run ccusage at a time
14
+ # - Other processes should skip execution gracefully
15
+ # - No process pile-up or resource leaks should occur
16
+
17
+ set -euo pipefail
18
+
19
+ # Mock JSON input handling
20
+ json_input=$(cat)
21
+
22
+ # Implement file-based locking to prevent concurrent executions
23
+ LOCK_FILE="/tmp/ccusage_statusline.lock"
24
+ LOCK_PID_FILE="/tmp/ccusage_statusline.pid"
25
+
26
+ # Function to check if process is still running
27
+ is_process_running() {
28
+ local pid=$1
29
+ if [ -z "$pid" ]; then return 1; fi
30
+ kill -0 "$pid" 2>/dev/null
31
+ }
32
+
33
+ # Try to acquire lock
34
+ if mkdir "$LOCK_FILE" 2>/dev/null; then
35
+ # Lock acquired, save our PID
36
+ echo $$ > "$LOCK_PID_FILE"
37
+
38
+ # Run ccusage with timeout (simulated with echo for testing)
39
+ echo "[$$] Running ccusage..." >&2
40
+
41
+ # Simulate ccusage work
42
+ sleep 0.5
43
+
44
+ # Generate mock output
45
+ echo "📁 ~/test 🌿 main 🤖 Claude [simulated from $$]"
46
+
47
+ # Clean up lock
48
+ rm -f "$LOCK_PID_FILE" 2>/dev/null
49
+ rmdir "$LOCK_FILE" 2>/dev/null
50
+ else
51
+ # Lock exists, check if it's stale
52
+ if [ -f "$LOCK_PID_FILE" ]; then
53
+ old_pid=$(cat "$LOCK_PID_FILE" 2>/dev/null)
54
+ if ! is_process_running "$old_pid"; then
55
+ # Stale lock, clean it up
56
+ echo "[$$] Cleaning stale lock from $old_pid" >&2
57
+ rm -f "$LOCK_PID_FILE" 2>/dev/null
58
+ rmdir "$LOCK_FILE" 2>/dev/null
59
+ else
60
+ echo "[$$] Skipped - lock held by $old_pid" >&2
61
+ fi
62
+ else
63
+ echo "[$$] Skipped - lock exists" >&2
64
+ fi
65
+ # Return cached/default output when skipped
66
+ echo "📁 ~/test 🌿 main 🤖 Claude [cached]"
67
+ fi