mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
auth: test concurrent authentication
This commit is contained in:
parent
1b1fabef8f
commit
4409932132
@ -15,9 +15,12 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/etcd/auth/authpb"
|
"github.com/coreos/etcd/auth/authpb"
|
||||||
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
||||||
@ -582,3 +585,49 @@ func contains(array []string, str string) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHammerSimpleAuthenticate(t *testing.T) {
|
||||||
|
// set TTL values low to try to trigger races
|
||||||
|
oldTTL, oldTTLRes := simpleTokenTTL, simpleTokenTTLResolution
|
||||||
|
defer func() {
|
||||||
|
simpleTokenTTL = oldTTL
|
||||||
|
simpleTokenTTLResolution = oldTTLRes
|
||||||
|
}()
|
||||||
|
simpleTokenTTL = 10 * time.Millisecond
|
||||||
|
simpleTokenTTLResolution = simpleTokenTTL
|
||||||
|
users := make(map[string]struct{})
|
||||||
|
|
||||||
|
as, tearDown := setupAuthStore(t)
|
||||||
|
defer tearDown(t)
|
||||||
|
|
||||||
|
// create lots of users
|
||||||
|
for i := 0; i < 50; i++ {
|
||||||
|
u := fmt.Sprintf("user-%d", i)
|
||||||
|
ua := &pb.AuthUserAddRequest{Name: u, Password: "123"}
|
||||||
|
if _, err := as.UserAdd(ua); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
users[u] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// hammer on authenticate with lots of users
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(len(users))
|
||||||
|
for u := range users {
|
||||||
|
go func(user string) {
|
||||||
|
defer wg.Done()
|
||||||
|
token := fmt.Sprintf("%s(%d)", user, i)
|
||||||
|
ctx := context.WithValue(context.WithValue(context.TODO(), "index", uint64(1)), "simpleToken", token)
|
||||||
|
if _, err := as.Authenticate(ctx, user, "123"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := as.AuthInfoFromCtx(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}(u)
|
||||||
|
}
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user