<commit_before>package net

import (
	"context"
	"net"
	"testing"
	"time"
)

func TestListenRangeConfig_Listen(t *testing.T) {
	topCtx, cancel := context.WithCancel(context.Background())
	defer cancel()

	var err error
	var lockedListener *Listener
	{ // open a random port in range
		ctx, cancel := context.WithTimeout(topCtx, time.Second*5)

		lockedListener, err = ListenRangeConfig{
			Min:  800,
			Max:  10000,
			Addr: "localhost",
		}.Listen(ctx)
		if err != nil {
			t.Fatalf("could not open first port")
		}
		cancel()
		defer lockedListener.Close() // in case
	}

	{ // open a second random port in range
		ctx, cancel := context.WithTimeout(topCtx, time.Second*5)

		listener, err := ListenRangeConfig{
			Min:  800,
			Max:  10000,
			Addr: "localhost",
		}.Listen(ctx)
		if err != nil {
			t.Fatalf("could not open first port")
		}
		cancel()
		if err := listener.Close(); err != nil { // in case
			t.Fatal("failed to close second random port")
		}
	}

	{ // test that opened port cannot be openned using min/max
		ctx, cancel := context.WithTimeout(topCtx, 250*time.Millisecond)

		l, err := ListenRangeConfig{
			Min: lockedListener.Port,
			Max: lockedListener.Port,
		}.Listen(ctx)
		if err == nil {
			l.Close()
			t.Fatal("port should be taken, this should fail")
		}
		if p := int(err.(ErrPortFileLocked)); p != lockedListener.Port {
			t.Fatalf("wrong fileport: %d", p)
		}
		cancel()
	}

	{ // test that opened port cannot be openned using min only
		ctx, cancel := context.WithTimeout(topCtx, 250*time.Millisecond)

		l, err := ListenRangeConfig{
			Min: lockedListener.Port,
		}.Listen(ctx)
		if err == nil {
			l.Close()
			t.Fatalf("port should be taken, this should timeout.")
		}
		if p := int(err.(ErrPortFileLocked)); p != lockedListener.Port {
			t.Fatalf("wrong fileport: %d", p)
		}
		cancel()
	}

	err = lockedListener.Close() // close port and release lock file
	if err != nil {
		t.Fatalf("could not release lockfile or port: %v", err)
	}
<commit_msg>configure_port_test.go: don't panic when error is not what we expected<commit_after>package net

import (
	"context"
	"net"
	"testing"
	"time"
)

func TestListenRangeConfig_Listen(t *testing.T) {
	topCtx, cancel := context.WithCancel(context.Background())
	defer cancel()

	var err error
	var lockedListener *Listener
	{ // open a random port in range
		ctx, cancel := context.WithTimeout(topCtx, time.Second*5)

		lockedListener, err = ListenRangeConfig{
			Min:  800,
			Max:  10000,
			Addr: "localhost",
		}.Listen(ctx)
		if err != nil {
			t.Fatalf("could not open first port")
		}
		cancel()
		defer lockedListener.Close() // in case
	}

	{ // open a second random port in range
		ctx, cancel := context.WithTimeout(topCtx, time.Second*5)

		listener, err := ListenRangeConfig{
			Min:  800,
			Max:  10000,
			Addr: "localhost",
		}.Listen(ctx)
		if err != nil {
			t.Fatalf("could not open first port")
		}
		cancel()
		if err := listener.Close(); err != nil { // in case
			t.Fatal("failed to close second random port")
		}
	}

	{ // test that opened port cannot be openned using min/max
		ctx, cancel := context.WithTimeout(topCtx, 250*time.Millisecond)

		l, err := ListenRangeConfig{
			Min: lockedListener.Port,
			Max: lockedListener.Port,
		}.Listen(ctx)
		if err == nil {
			l.Close()
			t.Fatal("port should be taken, this should fail")
		}
		if p := int(err.(ErrPortFileLocked)); p != lockedListener.Port {
			t.Fatalf("wrong fileport: %d", p)
		}
		cancel()
	}

	{ // test that opened port cannot be openned using min only
		ctx, cancel := context.WithTimeout(topCtx, 250*time.Millisecond)

		l, err := ListenRangeConfig{
			Min: lockedListener.Port,
		}.Listen(ctx)
		if err == nil {
			l.Close()
			t.Fatalf("port should be taken, this should timeout.")
		}
		portErr, ok := err.(ErrPortFileLocked)
		if !ok {
			t.Fatalf("unexpected error: %v", err)
		}
		if p := int(portErr); p != lockedListener.Port {
			t.Fatalf("wrong fileport: %d", p)
		}
		cancel()
	}

	err = lockedListener.Close() // close port and release lock file
	if err != nil {
		t.Fatalf("could not release lockfile or port: %v", err)
	}

{'fraction_non_alphanumeric': 0.13726919339164237, 'fraction_numerical': 0.011661807580174927, 'mean_word_length': 3.3474128827877507, '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': '2866060', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1396, 'n_words': 420}