<commit_before>defmodule ProcessWatcherTest do
  use ExUnit.Case
  alias ChaosSpawn.ProcessWatcher

  setup do
    {:ok, watcher} = ChaosSpawn.ProcessWatcher.start_link
    {:ok, watcher: watcher}
  end

  test "recieves messages and stores those pids", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.all_pids(watcher) == [new_pid]
  end

  test "can query if a pid is being watched", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert watcher |> ProcessWatcher.contains_pid?(new_pid)
  end

  test "doesn't store dead pids", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    Process.exit(new_pid, :kill)
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.all_pids(watcher) == []
  end

  test "removes pids that die during storage", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.all_pids(watcher) != []
    Process.exit(new_pid, :kill)
    assert ProcessWatcher.all_pids(watcher) == []
  end


  test "doesn't store non-pids", %{watcher: watcher} do
    new_pid = "not a pid"
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.all_pids(watcher) == []
  end

  test "can request a random pid from the watcher", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.get_random_pid(watcher) == new_pid
  end

  test "random pid returns :none if no pids available", %{watcher: watcher} do
    assert ProcessWatcher.get_random_pid(watcher) == :none
  end

  test "never returns dead pids", %{watcher: watcher} do
    pid_one = spawn &ProcessWatcherTest.TestModule.test_fun/0
    pid_two = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(pid_one)
    watcher |> ProcessWatcher.add_pid(pid_two)
    send pid_one, {:die}
    assert ProcessWatcher.get_random_pid(watcher) == pid_two
  end

end

defmodule ProcessWatcherTest.TestModule do
  def test_fun do
    receive do
      {_} -> :ok
    end
  end
end
<commit_msg>add extra assert on process state to watcher test<commit_after>defmodule ProcessWatcherTest do
  use ExUnit.Case
  alias ChaosSpawn.ProcessWatcher

  setup do
    {:ok, watcher} = ChaosSpawn.ProcessWatcher.start_link
    {:ok, watcher: watcher}
  end

  test "recieves messages and stores those pids", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.all_pids(watcher) == [new_pid]
  end

  test "can query if a pid is being watched", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert watcher |> ProcessWatcher.contains_pid?(new_pid)
  end

  test "doesn't store dead pids", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    Process.exit(new_pid, :kill)
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.all_pids(watcher) == []
  end

  test "removes pids that die during storage", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.all_pids(watcher) != []
    Process.exit(new_pid, :kill)
    
    assert not Process.alive?(new_pid)
    assert ProcessWatcher.all_pids(watcher) == []
  end


  test "doesn't store non-pids", %{watcher: watcher} do
    new_pid = "not a pid"
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.all_pids(watcher) == []
  end

  test "can request a random pid from the watcher", %{watcher: watcher} do
    new_pid = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(new_pid)
    assert ProcessWatcher.get_random_pid(watcher) == new_pid
  end

  test "random pid returns :none if no pids available", %{watcher: watcher} do
    assert ProcessWatcher.get_random_pid(watcher) == :none
  end

  test "never returns dead pids", %{watcher: watcher} do
    pid_one = spawn &ProcessWatcherTest.TestModule.test_fun/0
    pid_two = spawn &ProcessWatcherTest.TestModule.test_fun/0
    watcher |> ProcessWatcher.add_pid(pid_one)
    watcher |> ProcessWatcher.add_pid(pid_two)
    send pid_one, {:die}
    assert ProcessWatcher.get_random_pid(watcher) == pid_two
  end

end

defmodule ProcessWatcherTest.TestModule do
  def test_fun do
    receive do
      {_} -> :ok
    end
  end
end

{'fraction_non_alphanumeric': 0.09790794979079498, 'fraction_numerical': 0.0029288702928870294, 'mean_word_length': 4.118843683083512, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3388261', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1649, 'n_words': 385}