diff --git a/test/Makefile b/test/Makefile index 1408d0c4921..4d824a88ed9 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,11 +1,13 @@ BINS = bin/random bin/multihash bin/ipfs bin/pollEndpoint bin/iptb bin/go-sleep +BINS += bin/go-timeout IPFS_ROOT = ../ IPFS_CMD = ../cmd/ipfs RANDOM_SRC = ../Godeps/_workspace/src/github.com/jbenet/go-random RANDOM_FILES_SRC = ../Godeps/_workspace/src/github.com/jbenet/go-random-files POLLENDPOINT_SRC= ../thirdparty/pollEndpoint GOSLEEP_SRC = ./dependencies/go-sleep +GOTIMEOUT_SRC = ./dependencies/go-timeout export PATH := ../bin:${PATH} @@ -48,6 +50,10 @@ bin/go-sleep: $(call find_go_files, $(GOSLEEP_SRC)) IPFS-BUILD-OPTIONS @echo "*** installing $@ ***" go build $(GOFLAGS) -o bin/go-sleep $(GOSLEEP_SRC) +bin/go-timeout: $(call find_go_files, $(GOTIMEOUT_SRC)) IPFS-BUILD-OPTIONS + @echo "*** installing $@ ***" + go build $(GOFLAGS) -o bin/go-timeout $(GOTIMEOUT_SRC) + # gx dependencies multihash_src: diff --git a/test/bin/.gitignore b/test/bin/.gitignore index 8eec62514ef..c032badeef4 100644 --- a/test/bin/.gitignore +++ b/test/bin/.gitignore @@ -8,4 +8,3 @@ !checkflags !continueyn !verify-go-fmt.sh -!time-out diff --git a/test/bin/time-out b/test/bin/time-out deleted file mode 100755 index 47d863bd0f5..00000000000 --- a/test/bin/time-out +++ /dev/null @@ -1,83 +0,0 @@ -#!/bin/bash -# -# The Bash shell script executes a command with a time-out. -# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal -# is blocked, then the subsequent SIGKILL (9) terminates it. -# -# Based on the Bash documentation example. - -scriptName="${0##*/}" - -declare -i DEFAULT_TIMEOUT=9 -declare -i DEFAULT_INTERVAL=1 -declare -i DEFAULT_DELAY=1 - -# Timeout. -declare -i timeout=DEFAULT_TIMEOUT -# Interval between checks if the process is still alive. -declare -i interval=DEFAULT_INTERVAL -# Delay between posting the SIGTERM signal and destroying the process by SIGKILL. -declare -i delay=DEFAULT_DELAY - -function printUsage() { - cat < 0)); do - sleep $interval - kill -0 $$ || exit 0 - ((t -= interval)) - done - - # Be nice, post SIGTERM first. - # The 'exit 0' below will be executed if any preceeding command fails. - kill -s SIGTERM $$ && kill -0 $$ || exit 0 - sleep $delay - kill -s SIGKILL $$ -) 2> /dev/null & - -exec "$@" diff --git a/test/dependencies/go-timeout/.gitignore b/test/dependencies/go-timeout/.gitignore new file mode 100644 index 00000000000..46c758b1daa --- /dev/null +++ b/test/dependencies/go-timeout/.gitignore @@ -0,0 +1 @@ +go-timeout diff --git a/test/dependencies/go-timeout/LICENSE b/test/dependencies/go-timeout/LICENSE new file mode 100644 index 00000000000..5c30e8cce46 --- /dev/null +++ b/test/dependencies/go-timeout/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2016 Jakub "Kubuxu" Sztandera + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/test/dependencies/go-timeout/main.go b/test/dependencies/go-timeout/main.go new file mode 100644 index 00000000000..326267c8890 --- /dev/null +++ b/test/dependencies/go-timeout/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "context" + "fmt" + "os" + "os/exec" + "strconv" + "syscall" + "time" +) + +func main() { + if len(os.Args) < 3 { + fmt.Fprintf(os.Stderr, + "Usage: %s \n", os.Args[0]) + os.Exit(1) + } + timeout, err := strconv.ParseUint(os.Args[1], 10, 32) + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } + ctx, _ := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) + + cmd := exec.CommandContext(ctx, os.Args[2], os.Args[3:]...) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err = cmd.Start() + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + } + err = cmd.Wait() + + if err != nil { + if ctx.Err() != nil { + os.Exit(124) + } else { + exitErr, ok := err.(*exec.ExitError) + if !ok { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(255) + } + waits, ok := exitErr.Sys().(syscall.WaitStatus) + if !ok { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(255) + } + os.Exit(waits.ExitStatus()) + } + } else { + os.Exit(0) + } +} diff --git a/test/sharness/Makefile b/test/sharness/Makefile index 4e6d2a940f8..2c5488dcbc0 100644 --- a/test/sharness/Makefile +++ b/test/sharness/Makefile @@ -8,7 +8,7 @@ T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh)) BINS = bin/random bin/multihash bin/ipfs bin/pollEndpoint \ - bin/iptb bin/go-sleep bin/random-files + bin/iptb bin/go-sleep bin/random-files bin/go-timeout SHARNESS = lib/sharness/sharness.sh IPFS_ROOT = ../.. diff --git a/test/sharness/t0500-issues-and-regressions-offline.sh b/test/sharness/t0500-issues-and-regressions-offline.sh index 802adea6a8a..bfab467e53e 100755 --- a/test/sharness/t0500-issues-and-regressions-offline.sh +++ b/test/sharness/t0500-issues-and-regressions-offline.sh @@ -8,17 +8,17 @@ test_description="Tests for various fixed issues and regressions." test_expect_success "ipfs init with occupied input works - #2748" ' export IPFS_PATH="ipfs_path" - echo "" | time-out ipfs init && + echo "" | go-timeout 10 ipfs init && rm -rf ipfs_path ' test_init_ipfs -test_expect_success "ipfs cat --help succeeds with no input" ' - time-out ipfs cat --help +test_expect_success "ipfs cat --help succeeds when input remains open" ' + yes | go-timeout 1 ipfs cat --help ' -test_expect_success "ipfs pin ls --help succeeds with no input" ' - time-out ipfs pin ls --help +test_expect_success "ipfs pin ls --help succeeds when input remains open" ' + yes | go-timeout 1 ipfs pin ls --help ' test_expect_success "ipfs add on 1MB from stdin woks" '