提交 0e5e6cf9 作者: Jeromy Johnson 提交者: GitHub

Merge pull request #3152 from ipfs/feat/test/time-out-license

test: change time-out script to one from coreutils
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:
......
......@@ -8,4 +8,3 @@
!checkflags
!continueyn
!verify-go-fmt.sh
!time-out
#!/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 <<EOF
Synopsis
$scriptName [-t timeout] [-i interval] [-d delay] command
Execute a command with a time-out.
Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM
signal is blocked, then the subsequent SIGKILL (9) terminates it.
-t timeout
Number of seconds to wait for command completion.
Default value: $DEFAULT_TIMEOUT seconds.
-i interval
Interval between checks if the process is still alive.
Positive integer, default value: $DEFAULT_INTERVAL seconds.
-d delay
Delay between posting the SIGTERM signal and destroying the
process by SIGKILL. Default value: $DEFAULT_DELAY seconds.
As of today, Bash does not support floating point arithmetic (sleep does),
therefore all delay/time values must be integers.
EOF
}
# Options.
while getopts ":t:i:d:" option; do
case "$option" in
t) timeout=$OPTARG ;;
i) interval=$OPTARG ;;
d) delay=$OPTARG ;;
*) printUsage; exit 1 ;;
esac
done
shift $((OPTIND - 1))
# $# should be at least 1 (the command to execute), however it may be strictly
# greater than 1 if the command itself has options.
if (($# == 0 || interval <= 0)); then
printUsage
exit 1
fi
# kill -0 pid Exit code indicates if a signal may be sent to $pid process.
(
((t = timeout))
while ((t > 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 "$@"
The MIT License (MIT)
Copyright (c) 2016 Jakub "Kubuxu" Sztandera <k.sztandera@protonmail.ch>
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.
package main
import (
"context"
"fmt"
"os"
"os/exec"
"strconv"
"syscall"
"time"
)
func main() {
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr,
"Usage: %s <timeout-in-sec> <command ...>\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)
}
}
......@@ -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 = ../..
......
......@@ -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" '
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论