Skip to content
quarry Artifact [dbb2924de1]
Login

Artifact [dbb2924de1]

Artifact dbb2924de188f95b6e732b3b7e347bc5e85eb17e56b05d9252078b0038b78f5b:


package streamcap

import "testing"

// One address may hold only its share; the next is refused until one
// of its streams ends.
func TestAnAddressIsCappedAndGetsItsSlotBack(t *testing.T) {
	l := New(2, 10)
	r1, ok1 := l.Acquire("a")
	_, ok2 := l.Acquire("a")
	if !ok1 || !ok2 {
		t.Fatal("FAIL: an address was refused below its cap")
	}
	if _, ok := l.Acquire("a"); ok {
		t.Fatal("FAIL: an address was admitted past its cap")
	}
	if _, ok := l.Acquire("b"); !ok {
		t.Fatal("FAIL: a second address was refused for the first one's streams")
	}
	r1()
	if _, ok := l.Acquire("a"); !ok {
		t.Fatal("FAIL: an ended stream did not give its address the slot back")
	}
	t.Log("ok: per-address cap refuses at N+1 and releases")
}

// The total holds however many addresses the streams come from.
func TestTheTotalHoldsAcrossAddresses(t *testing.T) {
	l := New(5, 3)
	var releases []func()
	for _, a := range []string{"a", "b", "c"} {
		r, ok := l.Acquire(a)
		if !ok {
			t.Fatalf("FAIL: %s refused below the total", a)
		}
		releases = append(releases, r)
	}
	if _, ok := l.Acquire("d"); ok {
		t.Fatal("FAIL: a fourth address was admitted past the total")
	}
	releases[0]()
	releases[0]() // a second release must not free a slot it does not hold
	if _, ok := l.Acquire("d"); !ok {
		t.Fatal("FAIL: an ended stream did not return its slot to the total")
	}
	if _, ok := l.Acquire("e"); ok {
		t.Fatal("FAIL: releasing twice freed two slots")
	}
	t.Log("ok: total cap holds across addresses; release is idempotent")
}