aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/password_hasher_test.go (928B)


      1 package main
      2 
      3 import (
      4 	"sync"
      5 	"testing"
      6 	"time"
      7 )
      8 
      9 // Keep an accepted request's reply pending so later derivations wait for the
     10 // worker. Tests can change credentials while a request is awaiting hashing.
     11 func blockPasswordHasher(t *testing.T, hasher *passwordHasher) func() {
     12 	t.Helper()
     13 	reply := make(chan []byte)
     14 	hasher.requests <- passwordRequest{salt: make([]byte, 16), reply: reply}
     15 	var once sync.Once
     16 	return func() { once.Do(func() { <-reply }) }
     17 }
     18 
     19 func TestPasswordHasherShutdownDrainsAcceptedDerivation(t *testing.T) {
     20 	hasher := newPasswordHasher()
     21 	release := blockPasswordHasher(t, hasher)
     22 	defer release()
     23 	stopped := make(chan struct{})
     24 	go func() { hasher.stop(); close(stopped) }()
     25 	select {
     26 	case <-stopped:
     27 		t.Fatal("shutdown discarded an accepted derivation")
     28 	default:
     29 	}
     30 	release()
     31 	select {
     32 	case <-stopped:
     33 	case <-time.After(3 * time.Second):
     34 		t.Fatal("password worker outlived shutdown")
     35 	}
     36 }