summaryrefslogtreecommitdiff
path: root/test/files/run/t7350.scala
blob: 156d2f0719cf837d57c3585d6298f88e04af636a (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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import scala.sys.process.debug._
import scala.sys.process.{ProcessLogger, BasicIO}
import java.io.{Closeable, ByteArrayInputStream, ByteArrayOutputStream, InputStream, IOException}
import scala.concurrent.{Future, Await}
import scala.concurrent.duration.{Duration, SECONDS}
import scala.util.control.Exception._
import scala.concurrent.ExecutionContext.Implicits.global

object Test extends App {
  // Each test normally ends in a moment, but for a worst case, 
  // waits until 5 seconds and breaks off.

  // Tests for PipedProcesses

  println("PipedProcesses need not to release resources when it normally ends")

  {
    val io = BasicIO(false, ProcessLogger(_ => ()))
    val source = new PipeSourceMock
    val sink = new PipeSinkMock
    val a = new ProcessMock(error = false)
    val b = new ProcessMock(error = false)
    val p = new PipedProcesses(new ProcessBuilderMock(a, error = false), new ProcessBuilderMock(b, error = false), io, false)
    val f = Future {
      p.callRunAndExitValue(source, sink)
    }
    Await.result(f, Duration(5, SECONDS))
    println(s"- source.releaseCount: ${source.releaseCount}")
    println(s"- sink.releaseCount: ${sink.releaseCount}")
    println(s"- a.destroyCount: ${a.destroyCount}")
    println(s"- b.destroyCount: ${b.destroyCount}")
  }

  println("PipedProcesses must release resources when b.run() fails")

  {
    val io = BasicIO(false, ProcessLogger(_ => ()))
    val source = new PipeSourceMock
    val sink = new PipeSinkMock
    val a = new ProcessMock(error = false)
    val b = new ProcessMock(error = false)
    val p = new PipedProcesses(new ProcessBuilderMock(a, error = false), new ProcessBuilderMock(b, error = true), io, false)
    val f = Future {
      ignoring(classOf[IOException]) {
        p.callRunAndExitValue(source, sink)
      }
    }
    Await.result(f, Duration(5, SECONDS))
    println(s"- source.releaseCount: ${source.releaseCount}")
    println(s"- sink.releaseCount: ${sink.releaseCount}")
    println(s"- a.destroyCount: ${a.destroyCount}")
    println(s"- b.destroyCount: ${b.destroyCount}")
  }

  println("PipedProcesses must release resources when a.run() fails")

  {
    val io = BasicIO(false, ProcessLogger(_ => ()))
    val source = new PipeSourceMock
    val sink = new PipeSinkMock
    val a = new ProcessMock(error = false)
    val b = new ProcessMock(error = false)
    val p = new PipedProcesses(new ProcessBuilderMock(a, error = true), new ProcessBuilderMock(b, error = false), io, false)
    val f = Future {
      ignoring(classOf[IOException]) {
        p.callRunAndExitValue(source, sink)
      }
    }
    Await.result(f, Duration(5, SECONDS))
    println(s"- source.releaseCount: ${source.releaseCount}")
    println(s"- sink.releaseCount: ${sink.releaseCount}")
    println(s"- a.destroyCount: ${a.destroyCount}")
    println(s"- b.destroyCount: ${b.destroyCount}")
  }

  println("PipedProcesses must release resources when interrupted waiting for first.exitValue()")

  {
    val io = BasicIO(false, ProcessLogger(_ => ()))
    val source = new PipeSourceMock
    val sink = new PipeSinkMock
    val a = new ProcessMock(error = true)
    val b = new ProcessMock(error = false)
    val p = new PipedProcesses(new ProcessBuilderMock(a, error = false), new ProcessBuilderMock(b, error = false), io, false)
    val f = Future {
      p.callRunAndExitValue(source, sink)
    }
    Await.result(f, Duration(5, SECONDS))
    println(s"- source.releaseCount: ${source.releaseCount}")
    println(s"- sink.releaseCount: ${sink.releaseCount}")
    println(s"- a.destroyCount: ${a.destroyCount}")
    println(s"- b.destroyCount: ${b.destroyCount}")
  }

  println("PipedProcesses must release resources when interrupted waiting for second.exitValue()")

  {
    val io = BasicIO(false, ProcessLogger(_ => ()))
    val source = new PipeSourceMock
    val sink = new PipeSinkMock
    val a = new ProcessMock(error = false)
    val b = new ProcessMock(error = true)
    val p = new PipedProcesses(new ProcessBuilderMock(a, error = false), new ProcessBuilderMock(b, error = false), io, false)
    val f = Future {
      p.callRunAndExitValue(source, sink)
    }
    Await.result(f, Duration(5, SECONDS))
    println(s"- source.releaseCount: ${source.releaseCount}")
    println(s"- sink.releaseCount: ${sink.releaseCount}")
    println(s"- a.destroyCount: ${a.destroyCount}")
    println(s"- b.destroyCount: ${b.destroyCount}")
  }

  // Tests for PipeSource and PipeSink

  trait CloseChecking extends Closeable {
    var closed = false
    override def close() = closed = true
  }
  class DebugOutputStream extends ByteArrayOutputStream with CloseChecking
  class DebugInputStream(s: String) extends ByteArrayInputStream(s.getBytes()) with CloseChecking
  class DebugInfinityInputStream extends InputStream with CloseChecking {
    def read() = 1
  }

  def sourceSink() = {
    val source = new PipeSource
    val sink = new PipeSink
    source connectOut sink
    source.start()
    sink.start()
    (source, sink)
  }

  println("PipeSource and PipeSink must release resources when it normally ends")

  {
    val in = new DebugInputStream("aaa")
    val (source, sink) = sourceSink()
    val out = new DebugOutputStream
    source connectIn in
    sink connectOut out
    val f = Future {
      source.join()
      sink.join()
    }
    Await.result(f, Duration(5, SECONDS))
    println(s"- in.closed: ${in.closed}")
    println(s"- out.closed: ${out.closed}")
    println(s"- source.isReleased: ${source.isReleased}")
    println(s"- sink.isReleased: ${sink.isReleased}")
  }

  println("PipeSource and PipeSink must release resources when when waiting for source.take()")

  {
    val (source, sink) = sourceSink()
    val out = new DebugOutputStream
    sink connectOut out
    val f = Future {
      sink.ensureRunloopStarted()
      source.release()
      sink.release()
    }
    Await.result(f, Duration(5, SECONDS))
    println(s"- out.closed: ${out.closed}")
    println(s"- source.isReleased: ${source.isReleased}")
    println(s"- sink.isReleased: ${sink.isReleased}")
  }

  println("PipeSource and PipeSink must release resources when when waiting for sink.take()")

  {
    val in = new DebugInputStream("aaa")
    val (source, sink) = sourceSink()
    source connectIn in
    val f = Future {
      source.ensureRunloopStarted()
      source.release()
      sink.release()
    }
    Await.result(f, Duration(5, SECONDS))
    println(s"- in.closed: ${in.closed}")
    println(s"- source.isReleased: ${source.isReleased}")
    println(s"- sink.isReleased: ${sink.isReleased}")
  }

  println("PipeSource and PipeSink must release resources when copying stream")

  {
    val in = new DebugInfinityInputStream
    val (source, sink) = sourceSink()
    val out = new DebugOutputStream
    source connectIn in
    sink connectOut out
    val f = Future {
      source.ensureRunloopStarted()
      sink.ensureRunloopStarted()
      source.release()
      sink.release()
    }
    Await.result(f, Duration(5, SECONDS))
    println(s"- in.closed: ${in.closed}")
    println(s"- out.closed: ${out.closed}")
    println(s"- source.isReleased: ${source.isReleased}")
    println(s"- sink.isReleased: ${sink.isReleased}")
  }
}

package scala.sys.process {
  import java.io._
  import java.lang.reflect.InvocationTargetException

  object debug {

    class ProcessMock(error: Boolean) extends Process {
      var destroyCount = 0
      def exitValue(): Int = {
        if (error) {
          throw new InterruptedException()
        }
        0
      }
      def destroy(): Unit = { destroyCount += 1 }
    }

    class ProcessBuilderMock(process: Process, error: Boolean) extends ProcessBuilder.AbstractBuilder {
      override def run(io: ProcessIO): Process = {
        if (error) {
          throw new IOException()
        }
        process
      }
    }

    class PipeSinkMock extends Process.PipeSink("PipeSinkMock") {
      var releaseCount = 0
      override val pipe = null
      override val sink = null
      override def run(): Unit = {}
      override def connectOut(out: OutputStream): Unit = {}
      override def connectIn(pipeOut: PipedOutputStream): Unit = {}
      override def release(): Unit = { releaseCount += 1 }
    }

    class PipeSourceMock extends Process.PipeSource("PipeSourceMock") {
      var releaseCount = 0
      override val pipe = null
      override val source = null
      override def run(): Unit = {}
      override def connectIn(in: InputStream): Unit = {}
      override def connectOut(sink: Process.PipeSink): Unit = {}
      override def release(): Unit = { releaseCount += 1 }
    }

    class PipedProcesses(a: ProcessBuilder, b: ProcessBuilder, defaultIO: ProcessIO, toError: Boolean)
      extends Process.PipedProcesses(a, b, defaultIO, toError) {
      def callRunAndExitValue(source: Process.PipeSource, sink: Process.PipeSink) = {
        val m = classOf[Process.PipedProcesses].getDeclaredMethod("runAndExitValue", classOf[Process.PipeSource], classOf[Process.PipeSink])
        m.setAccessible(true)
        try m.invoke(this, source, sink).asInstanceOf[Option[Int]]
        catch {
          case err: InvocationTargetException => throw err.getTargetException
        }
      }
    }

    def throwsIOException(f: => Unit) = {
      try { f; false }
      catch { case _: IOException => true }
    }

    class PipeSink extends Process.PipeSink("TestPipeSink") {
      def ensureRunloopStarted() = {
        while (sink.size() > 0) {
          Thread.sleep(1)
        }
      }
      def isReleased = {
        val field = classOf[Process.PipeSink].getDeclaredField("pipe")
        field.setAccessible(true)
        val pipe = field.get(this).asInstanceOf[PipedInputStream]
        !this.isAlive && throwsIOException { pipe.read() }
      }
    }

    class PipeSource extends Process.PipeSource("TestPipeSource") {
      def ensureRunloopStarted() = {
        while (source.size() > 0) {
          Thread.sleep(1)
        }
      }
      def isReleased = {
        val field = classOf[Process.PipeSource].getDeclaredField("pipe")
        field.setAccessible(true)
        val pipe = field.get(this).asInstanceOf[PipedOutputStream]
        !this.isAlive && throwsIOException { pipe.write(1) }
      }
    }
  }
}