Newer
Older
dub_jkp / test / timeout.sh
@Geod24 Geod24 on 19 Jul 2022 1 KB test/timeout.sh: Fix broken pipes
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3.  
  4. . $(dirname "${BASH_SOURCE[0]}")/common.sh
  5.  
  6. PORT=$(getRandomPort)
  7.  
  8. log ' Testing unconnectable registry'
  9. if timeout 1s $DUB fetch dub --skip-registry=all --registry=http://localhost:$PORT; then
  10. die $LINENO 'Fetching from unconnectable registry should fail.'
  11. elif [ $? -eq 124 ]; then
  12. die $LINENO 'Fetching from unconnectable registry should fail immediately.'
  13. fi
  14.  
  15. log ' Testing non-responding registry'
  16. cat | nc -l $PORT >/dev/null &
  17. PID=$!
  18. if timeout 10s $DUB fetch dub --skip-registry=all --registry=http://localhost:$PORT; then
  19. die $LINENO 'Fetching from non-responding registry should fail.'
  20. elif [ $? -eq 124 ]; then
  21. die $LINENO 'Fetching from non-responding registry should time-out within 8s.'
  22. fi
  23. kill $PID 2>/dev/null || true
  24.  
  25. log ' Testing too slow registry'
  26. {
  27. res=$(printf 'HTTP/1.1 200 OK\r
  28. Server: dummy\r
  29. Content-Type: application/json\r
  30. Content-Length: 2\r
  31. \r
  32. {}')
  33. for i in $(seq 0 $((${#res} - 1))); do
  34. echo -n "${res:$i:1}" || true
  35. sleep 1
  36. done
  37. } | tail -n +1 | nc -l $PORT >/dev/null &
  38. PID=$!
  39. if timeout 10s time $DUB fetch dub --skip-registry=all --registry=http://localhost:$PORT; then
  40. die $LINENO 'Fetching from too slow registry should fail.'
  41. elif [ $? -eq 124 ]; then
  42. die $LINENO 'Fetching from too slow registry should time-out within 8s.'
  43. fi
  44. kill $PID 2>/dev/null || true