package main

import (
	"sync"
	"sync/atomic"
	"testing"
)

func TestPublished(t *testing.T) {
	if Published() != 42 {
		t.Fatal("publication failed")
	}
}
func TestLogicalRace(t *testing.T) {
	if got := BrokenReservation(); got != -1 {
		t.Fatal(got)
	}
}
func TestReserve(t *testing.T) {
	var stock, success atomic.Int64
	stock.Store(10)
	var wg sync.WaitGroup
	for range 100 {
		wg.Go(func() {
			if Reserve(&stock) {
				success.Add(1)
			}
		})
	}
	wg.Wait()
	if stock.Load() != 0 || success.Load() != 10 {
		t.Fatal(stock.Load(), success.Load())
	}
}
