<commit_before>let

  port = 4222;
  username = "client";
  password = "password";
  topic = "foo.bar";

in import ./make-test-python.nix ({ pkgs, lib, ... }: {
  name = "nats";
  meta = with pkgs.lib; { maintainers = with maintainers; [ c0deaddict ]; };

  nodes = let
    client = { pkgs, ... }: {
      environment.systemPackages = with pkgs; [ natscli ];
    };
  in {
    server = { pkgs, ... }: {
      networking.firewall.allowedTCPPorts = [ port ];
      services.nats = {
        inherit port;
        enable = true;
        settings = {
          authorization = {
            users = [{
              user = username;
              inherit password;
            }];
          };
        };
      };
    };

    client1 = client;
    client2 = client;
  };

  testScript = let file = "/tmp/msg";
  in ''
    def nats_cmd(*args):
        return (
            "nats "
            "--server=nats://server:${toString port} "
            "--user=${username} "
            "--password=${password} "
            "{}"
        ).format(" ".join(args))

    start_all()
    server.wait_for_unit("nats.service")

    client1.fail("test -f ${file}")

    # Subscribe on topic on client1 and echo messages to file.
    client1.execute("({} | tee ${file} &)".format(nats_cmd("sub", "--raw", "${topic}")))

    # Give client1 some time to subscribe.
    client1.execute("sleep 2")

    # Publish message on client2.
    client2.execute(nats_cmd("pub", "${topic}", "hello"))

    # Check if message has been received.
    client1.succeed("grep -q hello ${file}")<commit_msg>nixos/nats: fix test <commit_after>let

  port = 4222;
  username = "client";
  password = "password";
  topic = "foo.bar";

in import ./make-test-python.nix ({ pkgs, lib, ... }: {
  name = "nats";
  meta = with pkgs.lib; { maintainers = with maintainers; [ c0deaddict ]; };

  nodes = let
    client = { pkgs, ... }: {
      environment.systemPackages = with pkgs; [ natscli ];
    };
  in {
    server = { pkgs, ... }: {
      networking.firewall.allowedTCPPorts = [ port ];
      services.nats = {
        inherit port;
        enable = true;
        settings = {
          authorization = {
            users = [{
              user = username;
              inherit password;
            }];
          };
        };
      };
    };

    client1 = client;
    client2 = client;
  };

  testScript = let file = "/tmp/msg";
  in ''
    def nats_cmd(*args):
        return (
            "nats "
            "--server=nats://server:${toString port} "
            "--user=${username} "
            "--password=${password} "
            "{}"
        ).format(" ".join(args))

    def parallel(*fns):
        from threading import Thread
        threads = [ Thread(target=fn) for fn in fns ]
        for t in threads: t.start()
        for t in threads: t.join()

    start_all()
    server.wait_for_unit("nats.service")

    with subtest("pub sub"):
        parallel(
            lambda: client1.succeed(nats_cmd("sub", "--count", "1", "${topic}")),
            lambda: client2.succeed("sleep 2 && {}".format(nats_cmd("pub", "${topic}", "hello"))),
        )
{'fraction_non_alphanumeric': 0.16517285531370038, 'fraction_numerical': 0.008642765685019206, 'mean_word_length': 1.8357531760435573, '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': '5483481', 'n_tokens_mistral': 1011, 'n_tokens_neox': 972, 'n_words': 221}