From 14084d512ec9b41096c37a2567687c7b745baa12 Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 7 Aug 2020 11:36:46 +0800 Subject: [PATCH] go --- .github/workflows/action.yml | 27 ++++ .gitignore | 4 + .travis.yml | 63 +++++--- azure-pipelines.yml | 168 +++++++++++++++++++++ httpx.nimble | 10 +- src/httpx.nim | 7 +- tests/megatest.nim | 0 tests/{tester.nim => not.nim} | 0 tests/{dispatcher.nim => start_server.nim} | 6 +- 9 files changed, 253 insertions(+), 32 deletions(-) create mode 100644 .github/workflows/action.yml create mode 100644 azure-pipelines.yml create mode 100644 tests/megatest.nim rename tests/{tester.nim => not.nim} (100%) rename tests/{dispatcher.nim => start_server.nim} (72%) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml new file mode 100644 index 0000000..ff62c38 --- /dev/null +++ b/.github/workflows/action.yml @@ -0,0 +1,27 @@ +name: Test Httpx + +on: + push: + branches: + - master + pull_request: + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: + - ubuntu-latest + - windows-latest + version: + - stable + steps: + - uses: actions/checkout@v1 + - uses: jiro4989/setup-nim-action@v1 + with: + nim-version: ${{ matrix.version }} + - name: Install Packages + run: nimble install -y + - name: Test command + run: nimble tests diff --git a/.gitignore b/.gitignore index f1d1222..4409180 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ /tests/* !/tests/*.nim !/tests/*.cfg +nimcache +testresults +.gitignore +outputGotten.txt diff --git a/.travis.yml b/.travis.yml index 43fafed..0a7f69e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,28 +1,53 @@ -os: - - linux - -dist: trusty - language: c cache: directories: - - "$HOME/.nimble" - - "$HOME/.choosenim" + - .cache + +matrix: + include: + # Build and test against the master (stable) and devel branches of Nim + - os: linux + env: CHANNEL=stable + compiler: gcc + + # On OSX we only test against clang (gcc is mapped to clang by default) + - os: osx + env: CHANNEL=stable + compiler: clang + + - os: windows + env: CHANNEL=stable + compiler: gcc + + + allow_failures: + # Ignore failures when building against the devel Nim branch + # Also ignore OSX, due to very long build queue + - os: windows + + fast_finish: true + +# BEGIN: Assuming you rely on external dependencies +# addons: # This will only be executed on Linux +# apt: +# packages: +# - libzip-dev + +# before_install: +# # If you want to install an OSX Homebrew dependency +# - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi +# - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install libzip; fi +# ## END: Assuming you rely on external dependencies install: - - | - curl https://nim-lang.org/choosenim/init.sh -sSf > init.sh - sh init.sh -y - -before_script: - - set -e - - set -x - - export PATH=$HOME/.nimble/bin:$PATH - export CHOOSENIM_NO_ANALYTICS=1 + - curl https://nim-lang.org/choosenim/init.sh -sSf > init.sh + - sh init.sh -y + - export PATH=~/.nimble/bin:$PATH + - echo "export PATH=~/.nimble/bin:$PATH" >> ~/.profile + - choosenim $CHANNEL script: - - nimble test - -notifications: - irc: "chat.freenode.net#nimbuild" + - nimble install -y + - nimble tests diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 0000000..f96f2b1 --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,168 @@ +strategy: + maxParallel: 10 + matrix: + Windows_stable_64bit: + VM: 'windows-latest' + UCPU: amd64 + CHANNEL: stable + TEST_LANG: c + Windows_devel_64bit: + VM: 'windows-latest' + UCPU: amd64 + CHANNEL: devel + TEST_LANG: c + Windows_cpp_devel_64bit: + VM: 'windows-latest' + UCPU: amd64 + CHANNEL: devel + TEST_LANG: cpp + Linux_stable_64bit: + VM: 'ubuntu-latest' + UCPU: amd64 + CHANNEL: stable + TEST_LANG: c + Linux_devel_64bit: + VM: 'ubuntu-latest' + UCPU: amd64 + CHANNEL: devel + TEST_LANG: c + Linux_cpp_devel_64bit: + VM: 'ubuntu-latest' + UCPU: amd64 + CHANNEL: devel + TEST_LANG: cpp + # MacOS_stable_64bit: + # VM: 'macOS-latest' + # UCPU: amd64 + # CHANNEL: stable + # TEST_LANG: c + # MacOS_devel_64bit: + # VM: 'macOS-latest' + # UCPU: amd64 + # CHANNEL: devel + # TEST_LANG: c +pool: + vmImage: $(VM) + +steps: + - task: CacheBeta@1 + displayName: 'cache Nim binaries' + inputs: + key: NimBinaries | $(Agent.OS) | $(CHANNEL) | $(UCPU) + path: NimBinaries + + - task: CacheBeta@1 + displayName: 'cache MinGW-w64' + inputs: + key: mingwCache | 8_1_0 | $(UCPU) + path: mingwCache + condition: eq(variables['Agent.OS'], 'Windows_NT') + + - powershell: | + Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -Value 1 + displayName: 'long path support' + condition: eq(variables['Agent.OS'], 'Windows_NT') + - bash: | + echo "PATH=${PATH}" + set -e + echo "Installing MinGW-w64" + if [[ $UCPU == "i686" ]]; then + MINGW_FILE="i686-8.1.0-release-posix-dwarf-rt_v6-rev0.7z" + MINGW_URL="https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/8.1.0/threads-posix/dwarf/${MINGW_FILE}" + MINGW_DIR="mingw32" + else + MINGW_FILE="x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z" + MINGW_URL="https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/${MINGW_FILE}" + MINGW_DIR="mingw64" + fi + mkdir -p mingwCache + pushd mingwCache + if [[ ! -e "$MINGW_FILE" ]]; then + rm -f *.7z + curl -OLsS "$MINGW_URL" + fi + 7z x -y -bd "$MINGW_FILE" >/dev/null + mkdir -p /c/custom + mv "$MINGW_DIR" /c/custom/ + popd + # Workaround https://developercommunity.visualstudio.com/content/problem/891929/windows-2019-cygheap-base-mismatch-detected-git-ba.html + echo "##vso[task.prependpath]/usr/bin" + echo "##vso[task.prependpath]/mingw64/bin" + echo "##vso[task.setvariable variable=MINGW_DIR;]$MINGW_DIR" + displayName: 'Install dependencies (Windows)' + condition: eq(variables['Agent.OS'], 'Windows_NT') + - powershell: | + # export custom mingw PATH to other tasks + echo "##vso[task.prependpath]c:\custom\$(MINGW_DIR)\bin" + displayName: 'Mingw PATH (Windows)' + condition: eq(variables['Agent.OS'], 'Windows_NT') + - bash: | + echo "PATH=${PATH}" + export ncpu= + case '$(Agent.OS)' in + 'Linux') + ncpu=$(nproc) + ;; + 'Darwin') + ncpu=$(sysctl -n hw.ncpu) + ;; + 'Windows_NT') + ncpu=$NUMBER_OF_PROCESSORS + ;; + esac + [[ -z "$ncpu" || $ncpu -le 0 ]] && ncpu=1 + echo "Found ${ncpu} cores" + echo "##vso[task.setvariable variable=ncpu;]$ncpu" + displayName: 'Detecting number of cores' + - bash: | + echo "PATH=${PATH}" + gcc -v + export ucpu=${UCPU} + if [ "${CHANNEL}" = stable ]; then + BRANCH="v$(curl https://nim-lang.org/channels/stable)" + else + BRANCH="${CHANNEL}" + fi + mkdir -p NimBinaries + pushd NimBinaries + if [ ! -x "nim-${CHANNEL}/bin/nim" ]; then + git clone -b "${BRANCH}" https://github.com/nim-lang/nim "nim-${CHANNEL}/" + pushd "nim-${CHANNEL}" + git clone --depth 1 https://github.com/nim-lang/csources csources/ + pushd csources + make -j $ncpu ucpu=${UCPU} CC=gcc + popd + rm -rf csources + bin/nim c koch + ./koch boot -d:release + ./koch tools + else + pushd "nim-${CHANNEL}" + git fetch origin "${BRANCH}" + if [[ $(git merge FETCH_HEAD | grep -c "Already up to date.") -ne 1 ]]; then + bin/nim c koch + ./koch boot -d:release + ./koch tools + fi + fi + popd # exit nim-${CHANNEL} + popd # exit NimBinaries + displayName: 'Building Nim' + - powershell: | + echo "##vso[task.prependpath]$pwd\NimBinaries\nim-$(CHANNEL)\bin" + displayName: 'Set env variable (Windows)' + condition: eq(variables['Agent.OS'], 'Windows_NT') + - bash: | + echo "##vso[task.prependpath]$PWD/NimBinaries/nim-${CHANNEL}/bin" + displayName: 'Set env variable (Posix)' + condition: ne(variables['Agent.OS'], 'Windows_NT') + - bash: | + echo "PATH=${PATH}" + nimble refresh + nimble install -y + displayName: 'Building the package dependencies' + - bash: | + echo "PATH=${PATH}" + export ucpu=${UCPU} + nimble tests + displayName: 'Testing the package' diff --git a/httpx.nimble b/httpx.nimble index 3b2217b..5bced6d 100644 --- a/httpx.nimble +++ b/httpx.nimble @@ -12,14 +12,12 @@ srcDir = "src" requires "nim >= 1.2.0" requires "ioselectors >= 0.1.0" -# Test dependencies -requires "asynctools" task helloworld, "Compiles and executes the hello world server.": - exec "nim c -d:release --gc:boehm -r tests/helloworld" + exec "nim c -d:release -r tests/helloworld" task dispatcher, "Compiles and executes the dispatcher test server.": - exec "nim c -d:release --gc:boehm -r tests/dispatcher" + exec "nim c -d:release -r tests/dispatcher" -task test, "Runs the test suite.": - exec "nimble c -y -r tests/tester" +task tests, "Runs the test suite.": + exec "testament all" diff --git a/src/httpx.nim b/src/httpx.nim index 24e1525..66df2e6 100644 --- a/src/httpx.nim +++ b/src/httpx.nim @@ -8,6 +8,7 @@ from deques import len import ioselectors + when defined(windows): import sets else: @@ -422,11 +423,11 @@ proc ip*(req: Request): string = req.selector.getData(req.client).ip proc forget*(req: Request) = - ## Unregisters the underlying request's client socket from httpbeast's + ## Unregisters the underlying request's client socket from httpx's ## event loop. ## ## This is useful when you want to register ``req.client`` in your own - ## event loop, for example when wanting to integrate httpbeast into a + ## event loop, for example when wanting to integrate httpx into a ## websocket library. req.selector.unregister(req.client) @@ -481,7 +482,7 @@ proc run*(onRequest: OnRequest) {.inline.} = when false: proc close*(port: Port) = - ## Closes an httpbeast server that is running on the specified port. + ## Closes an httpx server that is running on the specified port. ## ## **NOTE:** This is not yet implemented. diff --git a/tests/megatest.nim b/tests/megatest.nim new file mode 100644 index 0000000..e69de29 diff --git a/tests/tester.nim b/tests/not.nim similarity index 100% rename from tests/tester.nim rename to tests/not.nim diff --git a/tests/dispatcher.nim b/tests/start_server.nim similarity index 72% rename from tests/dispatcher.nim rename to tests/start_server.nim index 75b7349..8e9c572 100644 --- a/tests/dispatcher.nim +++ b/tests/start_server.nim @@ -1,14 +1,12 @@ import options, asyncdispatch, httpclient -import httpbeast +import httpx proc onRequest(req: Request) {.async.} = if req.httpMethod == some(HttpGet): case req.path.get() of "/": - var client = newAsyncHttpClient() - let content = await client.getContent("http://localhost:8080/content") - req.send($content) + req.send("Hi World!") of "/content": req.send("Hi there!") else: