示例#1
0
func TestAsyncGetSet(t *testing.T) {
	mapCache := cachetest.NewMapCache()
	setCallCh := make(chan struct{})
	asyncCache := &Async{
		Cache: &Func{
			GetFunc: func(key string, params imageserver.Params) (*imageserver.Image, error) {
				return mapCache.Get(key, params)
			},
			SetFunc: func(key string, image *imageserver.Image, params imageserver.Params) error {
				err := mapCache.Set(key, image, params)
				setCallCh <- struct{}{}
				return err
			},
		},
	}

	err := asyncCache.Set("foo", testdata.Small, imageserver.Params{})
	if err != nil {
		panic(err)
	}
	<-setCallCh
	im, err := asyncCache.Get("foo", imageserver.Params{})
	if err != nil {
		panic(err)
	}
	if im == nil {
		t.Fatal("no image")
	}
}
示例#2
0
func TestServerErrorServer(t *testing.T) {
	s := &Server{
		Server:       &imageserver.StaticServer{Error: errors.New("error")},
		Cache:        cachetest.NewMapCache(),
		KeyGenerator: StringKeyGenerator("test"),
	}
	_, err := s.Get(imageserver.Params{})
	if err == nil {
		t.Fatal("no error")
	}
}
示例#3
0
func TestServerErrorServer(t *testing.T) {
	s := &Server{
		Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
			return nil, fmt.Errorf("error")
		}),
		Cache:        cachetest.NewMapCache(),
		KeyGenerator: StringKeyGenerator("test"),
	}
	_, err := s.Get(imageserver.Params{})
	if err == nil {
		t.Fatal("no error")
	}
}
示例#4
0
func TestServer(t *testing.T) {
	s := &Server{
		Server:       &imageserver.StaticServer{Image: testdata.Medium},
		Cache:        cachetest.NewMapCache(),
		KeyGenerator: StringKeyGenerator("test"),
	}
	image1, err := s.Get(imageserver.Params{})
	if err != nil {
		t.Fatal(err)
	}
	image2, err := s.Get(imageserver.Params{})
	if err != nil {
		t.Fatal(err)
	}
	if !imageserver.ImageEqual(image1, image2) {
		t.Fatal("not equal")
	}
}
示例#5
0
func TestServer(t *testing.T) {
	s := &Server{
		Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
			return testdata.Medium, nil
		}),
		Cache:        cachetest.NewMapCache(),
		KeyGenerator: StringKeyGenerator("test"),
	}
	image1, err := s.Get(imageserver.Params{})
	if err != nil {
		t.Fatal(err)
	}
	image2, err := s.Get(imageserver.Params{})
	if err != nil {
		t.Fatal(err)
	}
	if !imageserver.ImageEqual(image1, image2) {
		t.Fatal("not equal")
	}
}
示例#6
0
func TestIgnoreErrorGetSet(t *testing.T) {
	c := &IgnoreError{
		Cache: cachetest.NewMapCache(),
	}
	cachetest.TestGetSet(t, c)
}