#!/bin/bash

cvmfs_test_name="Overload cache with pinned catalogs"

cvmfs_run_test() {
  logfile=$1

  cvmfs_mount "atlas.cern.ch,atlas-nightlies.cern.ch,atlas-condb.cern.ch" \
    "CVMFS_QUOTA_LIMIT=1500" \
    "CVMFS_CHECK_PERMISSIONS=no" \
    "CVMFS_AUTO_UPDATE=no" || return 1

  RETVAL=0

  pid_before=$(sudo cvmfs_talk -i atlas.cern.ch pid)
  echo "*** pid of atlas.cern.ch fuse module (before stressing): $pid_before"

  pids_du=
  for d in /cvmfs/atlas.cern.ch/repo/sw/software/*; do
    if running_on_osx; then
      nohup du -ch -d 3 $d/ >/dev/null 2>&1 &
    else
      nohup du -ch --max-depth 3 $d/ >/dev/null 2>&1 &
    fi
    pids_du="$! $pids_du"
    echo "*** started new recursive listing on $d"
  done
  # Should be short enough to not exhaust 32bit inode limit on 32bit platforms
  echo "*** letting listings work for 15 minutes"
  sleep 900

  pid_after=$(sudo cvmfs_talk -i atlas.cern.ch pid)
  echo "*** pid of atlas.cern.ch fuse module (after stressing): $pid_after"
  if [ x"$pid_before" != x"$pid_after" ]; then
    echo "*** PID mismatch: pid before $pid_before, pid after $pid_after"
    echo "*** cat stacktrace file ($(date))"
    sudo cat "$(get_cvmfs_cachedir atlas.cern.ch)/stacktrace.atlas.cern.ch"
    RETVAL=2
  fi

  echo "*** killing recursive listing processes"
  sudo kill -9 $pids_du
  wait $pids_du

  return $RETVAL
}
