groupcache/http_test.go

289 lines
7.4 KiB
Go
Raw Normal View History

2013-07-24 04:57:40 +00:00
/*
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package groupcache
import (
"bytes"
"context"
2013-07-24 04:57:40 +00:00
"errors"
"flag"
"fmt"
2013-07-24 04:57:40 +00:00
"log"
"net"
"net/http"
"net/http/httptest"
2013-07-24 04:57:40 +00:00
"os"
"os/exec"
"strconv"
"strings"
"sync"
"testing"
"time"
2023-06-12 17:04:46 +00:00
"github.com/stretchr/testify/assert"
2013-07-24 04:57:40 +00:00
)
var (
peerAddrs = flag.String("test_peer_addrs", "", "Comma-separated list of peer addresses; used by TestHTTPPool")
peerIndex = flag.Int("test_peer_index", -1, "Index of which peer this child is; used by TestHTTPPool")
peerChild = flag.Bool("test_peer_child", false, "True if running as a child process; used by TestHTTPPool")
serverAddr = flag.String("test_server_addr", "", "Address of the server Child Getters will hit ; used by TestHTTPPool")
2013-07-24 04:57:40 +00:00
)
func TestHTTPPool(t *testing.T) {
if *peerChild {
beChildForTestHTTPPool(t)
2013-07-24 04:57:40 +00:00
os.Exit(0)
}
const (
nChild = 4
nGets = 100
)
var serverHits int
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello")
serverHits++
}))
defer ts.Close()
2013-07-24 04:57:40 +00:00
var childAddr []string
for i := 0; i < nChild; i++ {
childAddr = append(childAddr, pickFreeAddr(t))
}
var cmds []*exec.Cmd
var wg sync.WaitGroup
for i := 0; i < nChild; i++ {
cmd := exec.Command(os.Args[0],
"--test.run=TestHTTPPool",
"--test_peer_child",
"--test_peer_addrs="+strings.Join(childAddr, ","),
"--test_peer_index="+strconv.Itoa(i),
"--test_server_addr="+ts.URL,
2013-07-24 04:57:40 +00:00
)
cmds = append(cmds, cmd)
cmd.Stdout = os.Stdout
2013-07-24 04:57:40 +00:00
wg.Add(1)
if err := cmd.Start(); err != nil {
t.Fatal("failed to start child process: ", err)
}
go awaitAddrReady(t, childAddr[i], &wg)
}
defer func() {
for i := 0; i < nChild; i++ {
if cmds[i].Process != nil {
cmds[i].Process.Kill()
}
}
}()
wg.Wait()
// Use a dummy self address so that we don't handle gets in-process.
p := NewHTTPPool("should-be-ignored")
p.Set(addrToURL(childAddr)...)
// Dummy getter function. Gets should go to children only.
// The only time this process will handle a get is when the
// children can't be contacted for some reason.
getter := GetterFunc(func(ctx context.Context, key string, dest Sink) error {
2013-07-24 04:57:40 +00:00
return errors.New("parent getter called; something's wrong")
})
2019-05-30 22:14:47 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
2013-07-24 04:57:40 +00:00
g := NewGroup("httpPoolTest", 1<<20, getter)
for _, key := range testKeys(nGets) {
var value string
2019-05-30 22:14:47 +00:00
if err := g.Get(ctx, key, StringSink(&value)); err != nil {
2013-07-24 04:57:40 +00:00
t.Fatal(err)
}
if suffix := ":" + key; !strings.HasSuffix(value, suffix) {
t.Errorf("Get(%q) = %q, want value ending in %q", key, value, suffix)
}
t.Logf("Get key=%q, value=%q (peer:key)", key, value)
}
if serverHits != nGets {
t.Error("expected serverHits to equal nGets")
}
serverHits = 0
var value string
var key = "removeTestKey"
// Multiple gets on the same key
for i := 0; i < 2; i++ {
2019-05-30 22:14:47 +00:00
if err := g.Get(ctx, key, StringSink(&value)); err != nil {
t.Fatal(err)
}
}
// Should result in only 1 server get
if serverHits != 1 {
t.Error("expected serverHits to be '1'")
}
// Remove the key from the cache and we should see another server hit
2019-05-30 22:14:47 +00:00
if err := g.Remove(ctx, key); err != nil {
t.Fatal(err)
}
// Get the key again
2019-05-30 22:14:47 +00:00
if err := g.Get(ctx, key, StringSink(&value)); err != nil {
t.Fatal(err)
}
// Should register another server get
if serverHits != 2 {
t.Error("expected serverHits to be '2'")
}
key = "setMyTestKey"
setValue := []byte("test set")
// Add the key to the cache, optionally updating our local hot cache
if err := g.Set(ctx, key, setValue, time.Time{}, false); err != nil {
t.Fatal(err)
}
// Get the key
var getValue ByteView
if err := g.Get(ctx, key, ByteViewSink(&getValue)); err != nil {
t.Fatal(err)
}
if serverHits != 2 {
t.Errorf("expected serverHits to be '3' got '%d'", serverHits)
}
if !bytes.Equal(setValue, getValue.ByteSlice()) {
t.Fatal(errors.New(fmt.Sprintf("incorrect value retrieved after set: %s", getValue)))
}
// Key with non-URL characters to test URL encoding roundtrip
key = "a b/c,d"
if err := g.Get(ctx, key, StringSink(&value)); err != nil {
t.Fatal(err)
}
if suffix := ":" + key; !strings.HasSuffix(value, suffix) {
t.Errorf("Get(%q) = %q, want value ending in %q", key, value, suffix)
}
2023-06-12 17:04:46 +00:00
// Get a key that does not exist
2023-06-13 14:16:37 +00:00
err := g.Get(ctx, "IReturnErrNotFound", StringSink(&value))
2023-06-12 17:04:46 +00:00
errNotFound := &ErrNotFound{}
if !errors.As(err, &errNotFound) {
t.Fatal(errors.New("expected error to be 'ErrNotFound'"))
}
2023-06-13 14:16:37 +00:00
assert.Equal(t, "I am a ErrNotFound error", errNotFound.Error())
2023-06-12 17:04:46 +00:00
2023-06-13 14:16:37 +00:00
// Get a key that is guaranteed to return a remote error.
err = g.Get(ctx, "IReturnErrRemoteCall", StringSink(&value))
2023-06-12 17:04:46 +00:00
errRemoteCall := &ErrRemoteCall{}
if !errors.As(err, &errRemoteCall) {
t.Fatal(errors.New("expected error to be 'ErrRemoteCall'"))
}
2023-06-13 14:16:37 +00:00
assert.Equal(t, "I am a ErrRemoteCall error", errRemoteCall.Error())
2023-06-12 17:04:46 +00:00
2023-06-13 14:16:37 +00:00
// Get a key that is guaranteed to return an internal (500) error
err = g.Get(ctx, "IReturnInternalError", StringSink(&value))
assert.Equal(t, "I am a errors.New() error", err.Error())
2023-06-12 17:04:46 +00:00
2013-07-24 04:57:40 +00:00
}
func testKeys(n int) (keys []string) {
keys = make([]string, n)
for i := range keys {
keys[i] = strconv.Itoa(i)
}
return
}
func beChildForTestHTTPPool(t *testing.T) {
2013-07-24 04:57:40 +00:00
addrs := strings.Split(*peerAddrs, ",")
p := NewHTTPPool("http://" + addrs[*peerIndex])
p.Set(addrToURL(addrs)...)
getter := GetterFunc(func(ctx context.Context, key string, dest Sink) error {
2023-06-13 14:16:37 +00:00
if key == "IReturnErrNotFound" {
return &ErrNotFound{Msg: "I am a ErrNotFound error"}
2023-06-12 17:04:46 +00:00
}
2023-06-13 14:16:37 +00:00
if key == "IReturnErrRemoteCall" {
return &ErrRemoteCall{Msg: "I am a ErrRemoteCall error"}
}
if key == "IReturnInternalError" {
return errors.New("I am a errors.New() error")
2023-06-12 17:04:46 +00:00
}
if _, err := http.Get(*serverAddr); err != nil {
t.Logf("HTTP request from getter failed with '%s'", err)
}
2019-04-09 22:36:03 +00:00
dest.SetString(strconv.Itoa(*peerIndex)+":"+key, time.Time{})
2013-07-24 04:57:40 +00:00
return nil
})
NewGroup("httpPoolTest", 1<<20, getter)
log.Fatal(http.ListenAndServe(addrs[*peerIndex], p))
}
// This is racy. Another process could swoop in and steal the port between the
// call to this function and the next listen call. Should be okay though.
// The proper way would be to pass the l.File() as ExtraFiles to the child
// process, and then close your copy once the child starts.
func pickFreeAddr(t *testing.T) string {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
return l.Addr().String()
}
func addrToURL(addr []string) []string {
url := make([]string, len(addr))
for i := range addr {
url[i] = "http://" + addr[i]
}
return url
}
func awaitAddrReady(t *testing.T, addr string, wg *sync.WaitGroup) {
defer wg.Done()
const max = 1 * time.Second
tries := 0
for {
tries++
c, err := net.Dial("tcp", addr)
if err == nil {
c.Close()
return
}
delay := time.Duration(tries) * 25 * time.Millisecond
if delay > max {
delay = max
}
time.Sleep(delay)
}
}