<commit_before>defmodule GenServerringCounterTest do
  use ExUnit.Case
  doctest GenServerring

  defp update_state(state, action) do
    {mega, second, micro} = :erlang.now()
    dot = {node(), (mega * 1_000_000 + second) * 1_000_000 + micro}
    {:ok, state} = Crdtex.update(state, dot, action)
    state
  end

  defp setup_env(name) do
    Application.put_env(:gen_serverring, :name, name)
    Application.put_env(:gen_serverring, :callback, __MODULE__)
    :ok = Application.stop(:gen_serverring)
    :ok = Application.start(:gen_serverring)
  end

  defp inc(server), do: GenServerring.cast(server, :inc)

  defp dec(server), do: GenServerring.cast(server, :dec)

  defp get(server), do: GenServerring.call(server, :get)

  # callbacks for tests
  def init([]), do: {:ok, Crdtex.Counter.new}

  def handle_call(:get, _, counter), do: {:reply, Crdtex.value(counter), counter}

  def handle_cast(:inc,counter), do: {:noreply,update_state(counter,:increment)}
  def handle_cast(:dec,counter), do: {:noreply,update_state(counter,:decrement)}

  def handle_info(msg, counter) do
    send(:tester_handle_info, {:received, msg})
    {:noreply, counter}
  end

  def handle_state_change(counter) do
    send(:tester_handle_state_change, {:state_changed, Crdtex.value(counter)})
  end

  def handle_ring_change(up_set) do
    send(:tester_handle_ring_change, {:ring_changed, up_set})
  end

  # counter on one node only, this is just checking that the interaction between
  # the callback and GenServerring is working as expected.
  test "counter on one node, casts and calls" do
    name = :gen_serverring_counter_test
    setup_env(name)

    assert get(name) == 0<commit_msg>cleaning<commit_after>defmodule GenServerringCounterTest do
  use ExUnit.Case

  defp update_state(state, action) do
    {mega, second, micro} = :erlang.now()
    dot = {node(), (mega * 1_000_000 + second) * 1_000_000 + micro}
    {:ok, state} = Crdtex.update(state, dot, action)
    state
  end

  defp setup_env(name) do
    Application.put_env(:gen_serverring, :name, name)
    Application.put_env(:gen_serverring, :callback, __MODULE__)
    :ok = Application.stop(:gen_serverring)
    :ok = Application.start(:gen_serverring)
  end

  defp inc(server), do: GenServerring.cast(server, :inc)

  defp dec(server), do: GenServerring.cast(server, :dec)

  defp get(server), do: GenServerring.call(server, :get)

  # callbacks for tests
  def init([]), do: {:ok, Crdtex.Counter.new}

  def handle_call(:get,_,counter), do: {:reply,Crdtex.value(counter),counter}

  def handle_cast(:inc,counter), do: {:noreply,update_state(counter,:increment)}
  def handle_cast(:dec,counter), do: {:noreply,update_state(counter,:decrement)}

  def handle_info(msg, counter) do
    send(:tester_handle_info, {:received, msg})
    {:noreply, counter}
  end

  def handle_state_change(counter) do
    send(:tester_handle_state_change, {:state_changed, Crdtex.value(counter)})
  end

  def handle_ring_change(up_set) do
    send(:tester_handle_ring_change, {:ring_changed, up_set})
  end

  # counter on one node only, this is just checking that the interaction between
  # the callback and GenServerring is working as expected.
  test "counter on one node, casts and calls" do
    name = :gen_serverring_counter_test
    setup_env(name)

    assert get(name) == 0
{'fraction_non_alphanumeric': 0.12179680434127223, 'fraction_numerical': 0.00904431715405487, 'mean_word_length': 4.810858143607706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2254704', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1103, 'n_words': 276}