func TestSessionCacheParallel(t *testing.T) { var storage apitest.TrxStorage var auth = apitest.NewFakeAuth() cache := api.NewCache(auth.Auth) cache.Storage = &storage const ConcurrentAuths = 10 var wg sync.WaitGroup wg.Add(ConcurrentAuths) for i := 0; i < ConcurrentAuths; i++ { go func(i int) { defer wg.Done() cache.Auth(&api.AuthOptions{ User: &api.User{ Username: "******" + strconv.Itoa(i), Team: "foobar" + strconv.Itoa(i), }, }) }(i) } wg.Wait() if sessions := storage.Build(); !reflect.DeepEqual(sessions, auth.Sessions) { t.Fatalf("got %+v, want %+v", sessions, auth.Sessions) } if len(auth.Sessions) != ConcurrentAuths { t.Fatalf("want len(auth)=%d; got: %d", ConcurrentAuths, len(auth.Sessions)) } }
func TestSessionCache(t *testing.T) { var storage apitest.TrxStorage var auth = apitest.NewFakeAuth() users := []*api.Session{ {User: &api.User{Username: "******", Team: "foobar"}}, {User: &api.User{Username: "******", Team: "foobar"}}, {User: &api.User{Username: "******", Team: "foobar"}}, {User: &api.User{Username: "******", Team: "team"}}, } usernames := make(map[string]struct{}, len(users)) for _, user := range users { usernames[user.User.String()] = struct{}{} } cases := []struct { name string opts *api.AuthOptions // client trxs []apitest.Trx // underlying cache operations }{{ "new user1", &api.AuthOptions{ User: users[0].User, }, []apitest.Trx{ {Type: "get", Session: users[0]}, {Type: "set", Session: users[0]}, }, }, { "already cached user", &api.AuthOptions{ User: users[0].User, }, []apitest.Trx{ {Type: "get", Session: users[0]}, }, }, { "invalide session of a cached user", &api.AuthOptions{ User: users[0].User, Refresh: true, }, []apitest.Trx{ {Type: "delete", Session: users[0]}, {Type: "set", Session: users[0]}, }, }, { "new user2", &api.AuthOptions{ User: users[1].User, }, []apitest.Trx{ {Type: "get", Session: users[1]}, {Type: "set", Session: users[1]}, }, }, { "new user3", &api.AuthOptions{ User: users[2].User, }, []apitest.Trx{ {Type: "get", Session: users[2]}, {Type: "set", Session: users[2]}, }, }, { "new user", &api.AuthOptions{ User: users[3].User, }, []apitest.Trx{ {Type: "get", Session: users[3]}, {Type: "set", Session: users[3]}, }, }} cache := api.NewCache(auth.Auth) cache.Storage = &storage for _, cas := range cases { t.Run(cas.name, func(t *testing.T) { trxID := len(storage.Trxs) _, err := cache.Auth(cas.opts) if err != nil { t.Fatalf("Auth()=%s", err) } if err := storage.Slice(trxID).Match(cas.trxs); err != nil { t.Fatalf("Match()=%s", err) } }) } if sessions := storage.Build(); !reflect.DeepEqual(sessions, auth.Sessions) { t.Fatalf("got %+v, want %+v", sessions, auth.Sessions) } if len(auth.Sessions) != len(usernames) { t.Fatalf("want len(auth)=%d == len(allKeys)=%d", len(auth.Sessions), len(usernames)) } for username := range usernames { if _, ok := auth.Sessions[username]; !ok { t.Fatalf("username %q not found in auth", username) } } }
func TestTransport(t *testing.T) { var auth = apitest.NewFakeAuth() s, err := discovertest.NewServer(http.HandlerFunc(auth.GetSession)) if err != nil { t.Fatalf("NewServer()=%s", err) } defer s.Close() cache := api.NewCache(auth.Auth) users := []*api.Session{ {User: &api.User{Username: "******", Team: "foobar"}}, {User: &api.User{Username: "******", Team: "foobar"}}, {User: &api.User{Username: "******", Team: "foobar"}}, {User: &api.User{Username: "******", Team: "team"}}, } cases := []struct { name string sess *api.Session // client errs []error // fake errors for endpoint codes []int // fake response codes for endpoint err error // final error from client }{{ "new user1", users[0], nil, nil, nil, }, { "cached user1", users[0], nil, nil, nil, }, { "new user2", users[1], nil, nil, nil, }, { "new user3 with an error", users[2], []error{&net.DNSError{IsTemporary: true}}, nil, nil, }, { "cached user3 with an error and response codes", users[2], []error{&net.DNSError{IsTemporary: true}}, []int{401, 401}, nil, }} rec := &apitest.AuthRecorder{ AuthFunc: cache.Auth, } client := http.Client{ Transport: &api.Transport{ RoundTripper: &apitest.FakeTransport{}, AuthFunc: rec.Auth, }, } for _, cas := range cases { t.Run(cas.name, func(t *testing.T) { rec.Reset() req, err := http.NewRequest("POST", s.URL, strings.NewReader(cas.name)) if err != nil { t.Fatalf("NewRequest()=%s", err) } req = cas.sess.User.WithRequest(req) if len(cas.errs) != 0 { req = apitest.WithErrors(req, cas.errs...) } if len(cas.codes) != 0 { req = apitest.WithResponseCodes(req, cas.codes...) } resp, err := client.Do(req) if err != nil { t.Fatalf("Do()=%s", err) } if want, got := 1+len(cas.errs)+len(cas.codes), len(rec.Options); got != want { t.Fatalf("want %d, got %d: %+v", got, want, rec.Options) } if resp.StatusCode != http.StatusOK { t.Fatalf("got %q, want %q", http.StatusText(resp.StatusCode), http.StatusText(http.StatusOK)) } var other api.Session if err := json.NewDecoder(resp.Body).Decode(&other); err != nil { t.Fatalf("Decode()=%s", err) } if err := other.Valid(); err != nil { t.Fatalf("Valid()=%s", err) } if err := other.Match(cas.sess); err != nil { t.Fatalf("Match()=%s", err) } api.Log.Info("received session: %#v", &other) }) } }