From 367cc5ffb9a06ae9d22a198da6c9f8cf273f55b5 Mon Sep 17 00:00:00 2001 From: Jorge Vicente Cantero Date: Fri, 28 Jun 2019 01:14:20 +0200 Subject: [PATCH] Add test client works when server closes output stream --- .../java/sailgun/utils/SailgunStatusCode.java | 38 +++++++++++++++++++ core/src/test/scala/sailgun/SailgunSpec.scala | 6 +++ 2 files changed, 44 insertions(+) create mode 100644 core/src/test/java/sailgun/utils/SailgunStatusCode.java diff --git a/core/src/test/java/sailgun/utils/SailgunStatusCode.java b/core/src/test/java/sailgun/utils/SailgunStatusCode.java new file mode 100644 index 0000000..9ed21a1 --- /dev/null +++ b/core/src/test/java/sailgun/utils/SailgunStatusCode.java @@ -0,0 +1,38 @@ +/* + Copyright 2004-2012, Martian Software, Inc. + Copyright 2017-Present Facebook, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package sailgun.utils; + +import com.martiansoftware.nailgun.NGContext; + +/** Finish nail with provided exit code */ +public class SailgunStatusCode { + public static void nailMain(NGContext ctx) { + String[] args = ctx.getArgs(); + int exitCode = (int) ((Math.random() * 1000) + 1); + if (args.length > 0) { + try { + exitCode = Integer.parseInt(args[0]); + } catch (Exception e) { + } + } + // Close stdout to test the exit code is returned properly + // even in such case + System.out.close(); + ctx.exit(exitCode); + } +} \ No newline at end of file diff --git a/core/src/test/scala/sailgun/SailgunSpec.scala b/core/src/test/scala/sailgun/SailgunSpec.scala index 5835539..4aa7825 100644 --- a/core/src/test/scala/sailgun/SailgunSpec.scala +++ b/core/src/test/scala/sailgun/SailgunSpec.scala @@ -44,6 +44,12 @@ object SailgunSpec extends SailgunBaseSuite { ) } + testSailgun("closing stdout in nail doesn't affect client") { inputs => + val code = inputs.run("sailgun.utils.SailgunStatusCode", Array("0")) + assert(code == 0) + assertNoDiff(inputs.output, "") + } + val echoStdout = new PipedOutputStream() val echoStdin = new PipedInputStream(echoStdout) testSailgun("echo works (via stdin)", echoStdin) { inputs =>