How to wait for background process to start before running tests

I’m trying to migrate to CircleCI. The project is built using kotlin/java and gradle. It has a bunch of unit tests and some cucumber tests, these tests can be run using the ‘gradle test’ command.

An instance of the application is required to be run in the background for the cucumber tests to pass. I’ve tried to do this by running the application in the background and then running the tests. However, it looks like the tests get executed and fail before the background process has a chance to start. Any tips on how to get around this?

version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/openjdk:8-jdk

    working_directory: ~/repo

    environment:
      # Customize the JVM maximum heap limit
      JVM_OPTS: -Xmx3200m
      TERM: dumb
    
    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "build.gradle" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: gradle dependencies

      - save_cache:
          paths:
            - ~/.gradle
          key: v1-dependencies-{{ checksum "build.gradle" }}

      - run: gradle clean assemble jar
      - run: java -jar build/libs/definitions-catalogue-1.0.jar &

      - run:
          name: Run the application
          command: gradle run
          background: true
      # run tests
      - run: gradle test

Edit: I’ve tried to make the test re try the connection if it fails the first time (using Thread.Sleep()). However this appears to be ignored when the test is running.

The process would have been started by the time the very next step runs. I assume you want to know when it’s “ready”. That depends on what the process is doing.

I’d check for the presence of a file on the filesystem, a TCP port being opened, a certain line in a log file, etc that would indicated that the process is now ready.

Ta, that worked. I used a race condition checking port 5000 (web service)

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.