|
| 1 | +#@local scriptdir, write, checkPartialRead, process, c |
| 2 | +# |
| 3 | +gap> START_TEST("streamio.tst"); |
| 4 | +gap> scriptdir := DirectoriesLibrary( "tst/teststandard/processes/" );; |
| 5 | +gap> write := Filename(scriptdir, "slowwrite.sh");; |
| 6 | + |
| 7 | +# Read the output of 'prog', using 'readfunc'. The output should |
| 8 | +# come in two parts, 'firstread' and 'secondread'. |
| 9 | +# If GAP is too slow, allow 'firstread' and 'secondread' to be read |
| 10 | +# in one step, but then return 'false'. |
| 11 | +# We run this function multiple times to avoid very occasional failures |
| 12 | +# due to OS scheduling. |
| 13 | +gap> checkPartialRead := function(prog, readfunc, firstread, secondread) |
| 14 | +> local process, l; |
| 15 | +> process := InputOutputLocalProcess(DirectoryCurrent(), prog, ["prog", firstread, secondread]); |
| 16 | +> l := ReadLine(process); |
| 17 | +> if l = Concatenation(firstread, secondread) then |
| 18 | +> if ReadLine(process) <> fail then Error("Invalid end - type 1"); fi; |
| 19 | +> CloseStream(process); |
| 20 | +> return false; |
| 21 | +> else |
| 22 | +> if ReadLine(process) <> secondread then Error("Missing second"); fi; |
| 23 | +> if ReadLine(process) <> fail then Error("Invalid end - type 2"); fi; |
| 24 | +> CloseStream(process); |
| 25 | +> return true; |
| 26 | +> fi; |
| 27 | +> end;; |
| 28 | +gap> ForAny([1..10], x -> checkPartialRead(write, ReadLine, "aaa", "aaa\n")); |
| 29 | +true |
| 30 | +gap> ForAny([1..10], x -> checkPartialRead(write, ReadLine, "aaa", "aaa")); |
| 31 | +true |
| 32 | +gap> ForAny([1..10], x -> checkPartialRead(write, ReadAll, "aaa", "aaa\n")); |
| 33 | +true |
| 34 | + |
| 35 | +# Read as bytes, which is always identical |
| 36 | +gap> process := InputOutputLocalProcess(DirectoryCurrent(), write, ["prog", "abc", "def"]); |
| 37 | +< input/output stream to slowwrite.sh > |
| 38 | +gap> c := ReadByte(process); |
| 39 | +97 |
| 40 | +gap> c := ReadByte(process); |
| 41 | +98 |
| 42 | +gap> c := ReadByte(process); |
| 43 | +99 |
| 44 | +gap> c := ReadByte(process); |
| 45 | +100 |
| 46 | +gap> c := ReadByte(process); |
| 47 | +101 |
| 48 | +gap> c := ReadByte(process); |
| 49 | +102 |
| 50 | +gap> c := ReadByte(process); |
| 51 | +fail |
| 52 | +gap> CloseStream(process); |
| 53 | +gap> STOP_TEST("streamio.tst", 1); |
0 commit comments