# Flaky Circle CI Tests

**URL:** <https://discuss.circleci.com/t/flaky-circle-ci-tests/38534>\
**Category:** Build Environment\
**Created:** [December 16, 2020, 4:05pm UTC](https://discuss.circleci.com/t/flaky-circle-ci-tests/38534 "2020-12-16T16:05:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinceCarollo](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/vincecarollo/32/26236_2.png) [@VinceCarollo](https://discuss.circleci.com/u/VinceCarollo)\
**Post date:** [December 16, 2020, 4:05pm UTC](https://discuss.circleci.com/t/flaky-circle-ci-tests/38534/1 "2020-12-16T16:05:14Z")

</div>

Ruby: 2.6.3  
Rails: 5.2.4.4  
Rspec: 3.8.2

Our tests sweet passes locally, but when in our circle ci container we are seeing two main types of failures (although there are more):

- Persistent data through tests:
  - One of our tests creates a user, and check that User.count is 1. For some reason User.count in circle is 4.

- Odd format changes

```auto
expected: ([{:amount=>0.8215e2, :description=>"Ach Discount - 12/16/2020 - 12/31/2020", :item_code=>15, :type=>"credit_memo"}], {:account_id=>120})
         got: ([{:amount=>82.15, :description=>"Ach Discount - 12/16/2020 - 12/31/2020", :item_code=>15, :type=>"credit_memo"}], {:account_id=>120})

```

This is only happening to one particular branch, and all of these tests are passing locally. Any ideas as to what is causing this nonsense? This may very well be user error but after inspecting diffs over and over and not finding anything significant, I’m wondering if I could benefit from help from the community.

Circle config:

```yaml
version: 2
jobs:
  build:
    docker:
      - image: circleci/ruby:2.6.3-node
      - image: redis:3.2.9
      - image: circleci/postgres:10.3
    environment:
      RACK_ENV: test
      RAILS_ENV: test
      CIRCLE_TEST_REPORTS: test-reports
    parallelism: 8
    steps:
      - checkout
      - run:
          name: Switch timezone to Mountain
          command: sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/America/Boise /etc/localtime && date
      - run: sudo apt-get update
      - run: sudo apt install postgresql-client
      - run: mkdir -p tmp/cache $CIRCLE_TEST_REPORTS
      - run: npm install
      - run: gem install bundler
      - restore_cache:
          keys:
            - v1-dep-bundle-{{ checksum "Gemfile.lock" }}
            - v1-dep-bundle-
      - run: bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3
      - save_cache:
          key: v1-dep-bundle-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle
      - run:
          name: Database
          command: |
            mkdir -p config
            echo 'test:
              database: circle_ruby_test
              adapter: postgresql
              encoding: unicode
              pool: 25
              prepared_statements: false
              username: root
              host: localhost
            ' > config/database.yml
            bundle exec rake db:create db:structure:load --trace
      - run:
          name: Tests
          command: |
            TESTFILES=$(circleci tests glob "spec/requests/ **/*.rb" "spec/controllers/** /*.rb" "spec/integration/ **/*.rb" "spec/concerns/** /*.rb" "spec/services/ **/*.rb" "spec/helpers/** /*.rb" "spec/lib/ **/*.rb" "spec/mailers/** /*.rb" "spec/models/ **/*.rb" "spec/workers/** /*.rb"| circleci tests split --split-by=timings)
            echo ${TESTFILES}
            bundle exec rspec -r rspec_junit_formatter --format RspecJunitFormatter -o $CIRCLE_TEST_REPORTS/rspec/junit.xml -- ${TESTFILES}
          no_output_timeout: 15m
      - run: if [$CIRCLE_NODE_INDEX == 0] ; then bundle exec bundle-audit check --update --ignore CVE-<PRIVATE> ; fi
      - run: if [$CIRCLE_NODE_INDEX == 1] ; then bash ./script/syntax_check.sh ; fi
      - run: if [$CIRCLE_NODE_INDEX == 2] ; then bundle exec brakeman --exit-on-warn ; fi
      - add_ssh_keys
      - store_test_results:
          path: test-reports

```

Thanks for reviewing

---

<div class="post-metadata">

**Author:** ![simon-swanson](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/simon-swanson/32/24635_2.png) [@simon-swanson](https://discuss.circleci.com/u/simon-swanson)\
**Post date:** [January 4, 2021, 6:55pm UTC](https://discuss.circleci.com/t/flaky-circle-ci-tests/38534/2 "2021-01-04T18:55:33Z")

</div>

I don’t have a good answer for your formatting issues unfortunately. But for the user count issue, I think I know.

Parallelism on jobs runs _all_ of the steps on every executor, not just the testing step. You’ll either want to make your user creation idempotent, or rewrite the tests to account for multiple executors hitting the same database. From [the documentation](https://circleci.com/docs/2.0/parallelism-faster-jobs/) I wouldn’t have guessed this, but I’m pretty sure only the primary executor is duplicated for parallel jobs. Maybe someone else can chime in more authoritatively.

---

<div class="post-metadata">

**Author:** ![VinceCarollo](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/vincecarollo/32/26236_2.png) [@VinceCarollo](https://discuss.circleci.com/u/VinceCarollo)\
**Post date:** [January 11, 2021, 8:29pm UTC](https://discuss.circleci.com/t/flaky-circle-ci-tests/38534/3 "2021-01-11T20:29:00Z")

</div>

A recent pr introduced odd factory bot syntax that was somehow creating records rspec wasn’t tearing down 🤷‍♂️.

In one of our factories:

```auto
lead { FactoryBot.create(:lead) } 

```

Should’ve been:

```auto
association :lead

```

All is fixed thank you for your response.
