summaryrefslogtreecommitdiffstats
path: root/tests/cache-lock.test.ts
blob: c67ba0121f9c451e9011f9b782cfa7b063aad766 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { test, expect } from 'bun:test';
import { generateInstanceId } from '../scripts/cache/lock';
import type { CacheLock } from '../scripts/cache/utils';

test('generateInstanceId returns non-empty string', () => {
    const id = generateInstanceId();
    expect(typeof id).toBe('string');
    expect(id.length).toBeGreaterThan(0);
});

test('generateInstanceId includes timestamp', () => {
    const before = Date.now();
    const id = generateInstanceId();
    const after = Date.now();

    const timestampPart = id.split('-')[0];
    const timestamp = parseInt(timestampPart, 10);

    expect(timestamp).toBeGreaterThanOrEqual(before);
    expect(timestamp).toBeLessThanOrEqual(after);
});

test('generateInstanceId includes random component', () => {
    const id1 = generateInstanceId();
    const id2 = generateInstanceId();

    expect(id1).not.toBe(id2);
});

test('generateInstanceId format is timestamp-randomstring', () => {
    const id = generateInstanceId();
    const parts = id.split('-');
    expect(parts.length).toBe(2);

    const [timestamp, random] = parts;
    expect(timestamp).toMatch(/^\d+$/);
    expect(random).toMatch(/^[a-z0-9]+$/);
    expect(random.length).toBeGreaterThan(0);
    expect(random.length).toBeLessThan(10);
});

// Helper function to test isLockStale logic (since it's private)
function isLockStale(lock: CacheLock, currentHostname: string): boolean {
    const lockAge = Date.now() - lock.timestamp;
    const timeSinceRenewal = lock.renewedAt ? Date.now() - lock.renewedAt : lockAge;

    // Check 1: Timestamp-based staleness (30 minutes)
    if (timeSinceRenewal > 30 * 60 * 1000) {
        return true;
    }

    // Check 2: Process-based staleness (only on same machine)
    if (lock.hostname === currentHostname) {
        // For testing, assume process doesn't exist if pid is -1
        if (lock.pid === -1) {
            return true;
        }
    }

    return false;
}

test('isLockStale returns true for old lock (over 30 minutes)', () => {
    const lock: CacheLock = {
        locked: true,
        timestamp: Date.now() - 31 * 60 * 1000, // 31 minutes ago
        instance: 'test-instance',
        ttl: 30 * 60 * 1000,
        pid: 12345,
        hostname: 'test-host'
    };

    expect(isLockStale(lock, 'test-host')).toBe(true);
});

test('isLockStale returns false for recent lock (under 30 minutes)', () => {
    const lock: CacheLock = {
        locked: true,
        timestamp: Date.now() - 29 * 60 * 1000, // 29 minutes ago
        instance: 'test-instance',
        ttl: 30 * 60 * 1000,
        pid: 12345,
        hostname: 'test-host'
    };

    expect(isLockStale(lock, 'test-host')).toBe(false);
});

test('isLockStale respects renewedAt timestamp', () => {
    const now = Date.now();
    const lock: CacheLock = {
        locked: true,
        timestamp: now - 40 * 60 * 1000, // 40 minutes ago
        instance: 'test-instance',
        ttl: 30 * 60 * 1000,
        renewedAt: now - 10 * 60 * 1000, // Renewed 10 minutes ago
        pid: 12345,
        hostname: 'test-host'
    };

    expect(isLockStale(lock, 'test-host')).toBe(false);
});

test('isLockStale returns true when lock on same host but process dead', () => {
    const lock: CacheLock = {
        locked: true,
        timestamp: Date.now() - 10 * 60 * 1000, // 10 minutes ago
        instance: 'test-instance',
        ttl: 30 * 60 * 1000,
        pid: -1, // Simulate dead process
        hostname: 'test-host'
    };

    expect(isLockStale(lock, 'test-host')).toBe(true);
});

test('isLockStale returns false when lock on different host (even if old process)', () => {
    const lock: CacheLock = {
        locked: true,
        timestamp: Date.now() - 10 * 60 * 1000,
        instance: 'test-instance',
        ttl: 30 * 60 * 1000,
        pid: -1,
        hostname: 'different-host'
    };

    expect(isLockStale(lock, 'test-host')).toBe(false);
});

test('isLockStale handles missing renewedAt', () => {
    const lock: CacheLock = {
        locked: true,
        timestamp: Date.now() - 35 * 60 * 1000, // 35 minutes ago
        instance: 'test-instance',
        ttl: 30 * 60 * 1000,
        pid: 12345,
        hostname: 'test-host'
    };

    expect(isLockStale(lock, 'test-host')).toBe(true);
});