|
| 1 | +:- module(get_n_chars_tests, []). |
| 2 | +:- use_module(test_framework). |
| 3 | +:- use_module(library(charsio)). |
| 4 | +:- use_module(library(process)). |
| 5 | +:- use_module(library(format)). |
| 6 | +:- use_module(library(lists)). |
| 7 | + |
| 8 | +% Test 1: timeout=0 should behave exactly like get_n_chars/3 |
| 9 | +test("timeout=0 equals get_n_chars/3", ( |
| 10 | + atom_chars('/bin/echo', Echo), |
| 11 | + atom_chars('ABCDEFGHIJ', Content), |
| 12 | + process_create(Echo, [Content], [stdout(pipe(Out1))]), |
| 13 | + process_create(Echo, [Content], [stdout(pipe(Out2))]), |
| 14 | + |
| 15 | + get_n_chars(Out1, 5, Chars1), |
| 16 | + get_n_chars(Out2, 5, Chars2, 0), |
| 17 | + |
| 18 | + Chars1 = Chars2, |
| 19 | + close(Out1), |
| 20 | + close(Out2) |
| 21 | +)). |
| 22 | + |
| 23 | +% Test 2: Variable N with timeout=0 |
| 24 | +test("variable N with timeout=0", ( |
| 25 | + atom_chars('/bin/echo', Echo), |
| 26 | + atom_chars('Testing', Content), |
| 27 | + process_create(Echo, [Content], [stdout(pipe(Out1))]), |
| 28 | + process_create(Echo, [Content], [stdout(pipe(Out2))]), |
| 29 | + |
| 30 | + get_n_chars(Out1, N1, Chars1), |
| 31 | + get_n_chars(Out2, N2, Chars2, 0), |
| 32 | + |
| 33 | + N1 = N2, |
| 34 | + Chars1 = Chars2, |
| 35 | + N1 = 8, |
| 36 | + Chars1 = "Testing\n", |
| 37 | + close(Out1), |
| 38 | + close(Out2) |
| 39 | +)). |
| 40 | + |
| 41 | +% Test 3: Negative timeout should also mean no timeout |
| 42 | +test("negative timeout equals no timeout", ( |
| 43 | + atom_chars('/bin/echo', Echo), |
| 44 | + atom_chars('NegativeTest', Content), |
| 45 | + process_create(Echo, [Content], [stdout(pipe(Out1))]), |
| 46 | + process_create(Echo, [Content], [stdout(pipe(Out2))]), |
| 47 | + |
| 48 | + get_n_chars(Out1, N1, Chars1), |
| 49 | + get_n_chars(Out2, N2, Chars2, -100), |
| 50 | + |
| 51 | + N1 = N2, |
| 52 | + Chars1 = Chars2, |
| 53 | + close(Out1), |
| 54 | + close(Out2) |
| 55 | +)). |
| 56 | + |
| 57 | +% Test 4: Positive timeout should timeout on slow output |
| 58 | +test("positive timeout stops reading", ( |
| 59 | + atom_chars('/usr/bin/python3', Py), |
| 60 | + atom_chars('-c', C), |
| 61 | + atom_chars('import sys,time; [print(c,end="",flush=True) or time.sleep(1) for c in "ABCDEFGH"]', Cmd), |
| 62 | + process_create(Py, [C, Cmd], [stdout(pipe(Out))]), |
| 63 | + |
| 64 | + get_n_chars(Out, N, _Chars, 2500), |
| 65 | + |
| 66 | + N >= 2, |
| 67 | + N =< 3, |
| 68 | + close(Out) |
| 69 | +)). |
| 70 | + |
| 71 | +% Test 5: infinity atom means no timeout |
| 72 | +test("infinity atom means no timeout", ( |
| 73 | + atom_chars('/bin/echo', Echo), |
| 74 | + atom_chars('InfinityTest', Content), |
| 75 | + process_create(Echo, [Content], [stdout(pipe(Out))]), |
| 76 | + |
| 77 | + get_n_chars(Out, N, _Chars, infinity), |
| 78 | + |
| 79 | + N > 0, |
| 80 | + close(Out) |
| 81 | +)). |
| 82 | + |
| 83 | +% Test 6: Stream remains usable after timeout |
| 84 | +test("stream usable after timeout", ( |
| 85 | + atom_chars('/usr/bin/python3', Py), |
| 86 | + atom_chars('-c', C), |
| 87 | + atom_chars('import sys,time; print("A",end="",flush=True); time.sleep(2); print("B",end="",flush=True)', Cmd), |
| 88 | + process_create(Py, [C, Cmd], [stdout(pipe(Out))]), |
| 89 | + |
| 90 | + get_n_chars(Out, N1, Chars1, 100), |
| 91 | + get_n_chars(Out, N2, Chars2, 3000), |
| 92 | + |
| 93 | + N1 = 1, |
| 94 | + Chars1 = "A", |
| 95 | + N2 = 1, |
| 96 | + Chars2 = "B", |
| 97 | + close(Out) |
| 98 | +)). |
| 99 | + |
| 100 | +% Test 7: Timeout returns partial data, not EOF |
| 101 | +test("timeout returns partial data not EOF", ( |
| 102 | + atom_chars('/usr/bin/python3', Py), |
| 103 | + atom_chars('-c', C), |
| 104 | + atom_chars('import sys,time; print("ABC",end="",flush=True); time.sleep(5); print("DEF",end="",flush=True)', Cmd), |
| 105 | + process_create(Py, [C, Cmd], [stdout(pipe(Out))]), |
| 106 | + |
| 107 | + get_n_chars(Out, N1, Chars1, 1000), |
| 108 | + get_n_chars(Out, N2, Chars2, 6000), |
| 109 | + |
| 110 | + N1 = 3, |
| 111 | + Chars1 = "ABC", |
| 112 | + N2 = 3, |
| 113 | + Chars2 = "DEF", |
| 114 | + close(Out) |
| 115 | +)). |
| 116 | + |
| 117 | +% Test 8: Multiple reads with timeout=0 |
| 118 | +test("multiple reads with timeout=0", ( |
| 119 | + atom_chars('/bin/echo', Echo), |
| 120 | + atom_chars('ABCDEFGHIJKLMNOP', Content), |
| 121 | + process_create(Echo, [Content], [stdout(pipe(Out))]), |
| 122 | + |
| 123 | + get_n_chars(Out, 4, Chars1, 0), |
| 124 | + get_n_chars(Out, 4, Chars2, 0), |
| 125 | + get_n_chars(Out, 4, Chars3, 0), |
| 126 | + |
| 127 | + Chars1 = "ABCD", |
| 128 | + Chars2 = "EFGH", |
| 129 | + Chars3 = "IJKL", |
| 130 | + close(Out) |
| 131 | +)). |
| 132 | + |
| 133 | +% Test 9: Reading more than available with timeout=0 |
| 134 | +test("read more than available with timeout=0", ( |
| 135 | + atom_chars('/bin/echo', Echo), |
| 136 | + atom_chars('Short', Content), |
| 137 | + process_create(Echo, [Content], [stdout(pipe(Out))]), |
| 138 | + |
| 139 | + get_n_chars(Out, N, _Chars, 0), |
| 140 | + |
| 141 | + N >= 5, |
| 142 | + N =< 7, |
| 143 | + close(Out) |
| 144 | +)). |
| 145 | + |
| 146 | +% Test 10: Variable N unifies with actual character count |
| 147 | +test("variable N unifies with actual count", ( |
| 148 | + atom_chars('/usr/bin/python3', Py), |
| 149 | + atom_chars('-c', C), |
| 150 | + atom_chars('import sys,time; [print(c,end="",flush=True) or time.sleep(0.5) for c in "ABCD"]', Cmd), |
| 151 | + process_create(Py, [C, Cmd], [stdout(pipe(Out))]), |
| 152 | + |
| 153 | + get_n_chars(Out, N, Chars, 1300), |
| 154 | + length(Chars, ActualLength), |
| 155 | + |
| 156 | + N = ActualLength, |
| 157 | + close(Out) |
| 158 | +)). |
0 commit comments