summaryrefslogtreecommitdiff
path: root/tests/FundLab.Api.Tests/ProcessCollectorTests.fs
blob: ee3f016c7bd7de62b021aba4699bd1fc4cf34e99 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
namespace FundLab.Api.Tests

module ProcessCollectorTests =

    open System
    open System.Diagnostics
    open System.IO
    open System.Threading
    open System.Threading.Tasks
    open Xunit
    open FundLab.Api

    let private secretSentinel = "fund-lab-SECRET-SENTINEL"

    let private fixtureDir () =
        let dir = Path.Combine(Path.GetTempPath(), "fund-lab-collector-" + Guid.NewGuid().ToString("N"))

        Directory.CreateDirectory(dir) |> ignore

        dir

    let private writeScript (dir: string) (name: string) (body: string) =
        let path = Path.Combine(dir, name)
        File.WriteAllText(path, body)
        path

    let private searchEnvelope =
        "{\"schema_version\":\"fund-lab.akshare.v1\",\"operation\":\"search\",\"source\":\"fixture\",\"source_revision\":\"fixture/1\",\"collected_at\":\"2026-09-21T00:00:00Z\",\"_synthetic\":true,\"instruments\":[{\"code\":\"000001\",\"name\":\"fixture fund\",\"fund_type\":null}]}"

    let private hangScript dir =
        writeScript dir "hang.sh" ("sleep 60 & child=$!\necho $child > pidfile\nwait $child\n")

    let private orphanScript dir =
        writeScript
            dir
            "orphan.sh"
            ("sleep 30 & child=$!\necho $child > pidfile\nprintf '%s' '" + searchEnvelope + "'\nexit 0\n")

    let private recordedPid (dir: string) =
        Int32.Parse(File.ReadAllText(Path.Combine(dir, "pidfile")).Trim())

    let private pidAlive (pid: int) = Directory.Exists(sprintf "/proc/%d" pid)

    let private waitForPidDeath (pid: int) =
        let mutable dead = not (pidAlive pid)
        let mutable waited = 0

        while not dead && waited < 10000 do
            Thread.Sleep(100)
            waited <- waited + 100
            dead <- not (pidAlive pid)

        dead

    [<Fact>]
    let ``collector captures stdout from a successful subprocess`` () =
        let dir = fixtureDir ()

        try
            let script = writeScript dir "success.sh" ("printf '%s' '" + searchEnvelope + "'")
            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 30) :> IMarketDataCollector

            match collector.Search("000001", CancellationToken.None) with
            | Ok raw ->
                Assert.Contains("000001", raw)
                Assert.Contains("\"_synthetic\":true", raw)
            | Error message -> Assert.Fail(sprintf "expected success, got %s" message)
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``collector drains concurrent stderr flood without deadlocking`` () =
        let dir = fixtureDir ()

        try
            let body = "yes 'flood padding padding padding padding' | head -n 5000 >&2\nprintf '%s' '" + searchEnvelope + "'"

            let script = writeScript dir "flood.sh" body

            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 30) :> IMarketDataCollector

            match collector.Search("000001", CancellationToken.None) with
            | Ok raw -> Assert.Contains("000001", raw)
            | Error message -> Assert.Fail(sprintf "expected success, got %s" message)
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``collector enforces deadline, kills owned processes and sanitizes error`` () =
        let dir = fixtureDir ()

        try
            let script = hangScript dir
            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 1) :> IMarketDataCollector
            let stopwatch = Stopwatch.StartNew()

            match collector.Search("000001", CancellationToken.None) with
            | Error message ->
                stopwatch.Stop()
                Assert.Contains("did not finish within 1 seconds", message)
                Assert.DoesNotContain(dir, message)
                Assert.DoesNotContain(secretSentinel, message)
            | Ok raw -> Assert.Fail(sprintf "expected timeout error, got %s" raw)

            Assert.True(stopwatch.Elapsed < TimeSpan.FromSeconds(20.0))
            Assert.True(waitForPidDeath (recordedPid dir), "backgrounded subprocess survived the deadline kill")
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``collector reports cancellation and kills owned processes`` () =
        let dir = fixtureDir ()

        try
            let script = hangScript dir
            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 30) :> IMarketDataCollector
            use cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500.0))

            match collector.Search("000001", cts.Token) with
            | Error message ->
                Assert.Equal("collector run was cancelled", message)
                Assert.DoesNotContain(dir, message)
                Assert.DoesNotContain(secretSentinel, message)
            | Ok raw -> Assert.Fail(sprintf "expected cancellation, got %s" raw)

            Assert.True(waitForPidDeath (recordedPid dir), "backgrounded subprocess survived cancellation")
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``collector rejects orphan-held pipes at the total exit and EOF deadline`` () =
        let dir = fixtureDir ()

        try
            let script = orphanScript dir
            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 2) :> IMarketDataCollector
            let stopwatch = Stopwatch.StartNew()

            let result = collector.Search("000001", CancellationToken.None)
            stopwatch.Stop()

            match result with
            | Ok raw -> Assert.Fail(sprintf "recovered payload after expired deadline: %s" raw)
            | Error message ->
                Assert.Equal("collector did not finish within 2 seconds", message)
                Assert.DoesNotContain(dir, message)
                Assert.DoesNotContain(secretSentinel, message)

            Assert.True(stopwatch.Elapsed >= TimeSpan.FromSeconds(1.8))
            Assert.True(stopwatch.Elapsed < TimeSpan.FromSeconds(15.0))
            Assert.True(waitForPidDeath (recordedPid dir), "pipe-holding orphan survived the EOF deadline kill")
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``cancellation during EOF wait interrupts promptly and kills descendants`` () =
        let dir = fixtureDir ()

        try
            let script = orphanScript dir
            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 30) :> IMarketDataCollector
            use cts = new CancellationTokenSource()

            let searchTask = Task.Run(fun () -> collector.Search("000001", cts.Token))
            Thread.Sleep(700)

            let stopwatch = Stopwatch.StartNew()
            cts.Cancel()
            let completed = searchTask.Wait(10000)
            stopwatch.Stop()

            Assert.True(completed, "search did not return promptly after cancellation")

            match searchTask.Result with
            | Error message -> Assert.Equal("collector run was cancelled", message)
            | Ok raw -> Assert.Fail(sprintf "expected cancellation, got %s" raw)

            Assert.True(stopwatch.Elapsed < TimeSpan.FromSeconds(5.0), "cancellation during EOF wait was not prompt")
            Assert.True(waitForPidDeath (recordedPid dir), "pipe-holding orphan survived EOF-wait cancellation")
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``pre-cancelled request does not launch the collector`` () =
        let dir = fixtureDir ()

        try
            let script =
                writeScript dir "recording.sh" ("echo $$ > pidfile\nprintf '%s' '" + searchEnvelope + "'\n")

            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 30) :> IMarketDataCollector
            use cts = new CancellationTokenSource()
            cts.Cancel()

            match collector.Search("000001", cts.Token) with
            | Error message ->
                Assert.Equal("collector run was cancelled", message)
                Assert.DoesNotContain(dir, message)
            | Ok raw -> Assert.Fail(sprintf "expected pre-cancel, got %s" raw)

            Assert.False(File.Exists(Path.Combine(dir, "pidfile")), "collector process was launched despite pre-cancelled request")
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``collector surfaces malformed payload for parser to reject`` () =
        let dir = fixtureDir ()

        try
            let script = writeScript dir "malformed.sh" "printf '%s' '{ not json at all'"
            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 30) :> IMarketDataCollector

            match collector.Search("000001", CancellationToken.None) with
            | Ok raw ->
                Assert.Equal("{ not json at all", raw)

                match MarketData.parseSearchPayload raw with
                | Error _ -> ()
                | Ok _ -> Assert.Fail("expected malformed payload to fail parsing")
            | Error message -> Assert.Fail(sprintf "expected raw output, got error %s" message)
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``nonzero exit keeps stderr sentinel and paths out of the public error`` () =
        let dir = fixtureDir ()

        try
            let body =
                "printf 'secret=%s\\n' '"
                + secretSentinel
                + "' >&2\nprintf 'script lives under "
                + dir
                + "\\n' >&2\nexit 3\n"

            let script = writeScript dir "failing.sh" body
            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 30) :> IMarketDataCollector

            match collector.Search("000001", CancellationToken.None) with
            | Error message ->
                Assert.Equal("collector exited with code 3", message)
                Assert.DoesNotContain(secretSentinel, message)
                Assert.DoesNotContain(dir, message)
            | Ok raw -> Assert.Fail(sprintf "expected failure, got %s" raw)
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``start failure keeps executable path out of the public error`` () =
        let dir = fixtureDir ()

        try
            let script = writeScript dir "success.sh" ("printf '%s' '" + searchEnvelope + "'")
            let missingExecutable = "/nonexistent-" + secretSentinel + "/python3"

            let collector = ProcessMarketDataCollector(missingExecutable, script, None, 30) :> IMarketDataCollector

            match collector.Search("000001", CancellationToken.None) with
            | Error message ->
                let sanitized =
                    message = "could not run the AKShare collector"
                    || message.StartsWith("collector exited with code")

                Assert.True(sanitized, sprintf "unexpected error message: %s" message)
                Assert.DoesNotContain(secretSentinel, message)
                Assert.DoesNotContain(dir, message)
            | Ok raw -> Assert.Fail(sprintf "expected failure, got %s" raw)
        finally
            Directory.Delete(dir, true)

    [<Fact>]
    let ``missing collector script yields sanitized error without paths`` () =
        let dir = fixtureDir ()

        try
            let script = Path.Combine(dir, "missing.py")
            let collector = ProcessMarketDataCollector("/bin/sh", script, None, 30) :> IMarketDataCollector

            match collector.Search("000001", CancellationToken.None) with
            | Error message ->
                Assert.Equal("collector script is not deployed", message)
                Assert.DoesNotContain(dir, message)
            | Ok raw -> Assert.Fail(sprintf "expected error, got %s" raw)
        finally
            Directory.Delete(dir, true)