本文整理汇总了Golang中github.com/pierrre/imageserver.ServerFunc函数的典型用法代码示例。如果您正苦于以下问题:Golang ServerFunc函数的具体用法?Golang ServerFunc怎么用?Golang ServerFunc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ServerFunc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestServerErrorServer
func TestServerErrorServer(t *testing.T) {
srv := &Server{
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return nil, fmt.Errorf("error")
}),
}
_, err := srv.Get(imageserver.Params{Param: "source"})
if err == nil {
t.Fatal("no error")
}
}
示例2: TestServerErrorGroup
func TestServerErrorGroup(t *testing.T) {
srv := newTestServer(
imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return nil, fmt.Errorf("error")
}),
imageserver_cache.StringKeyGenerator("test"),
)
_, err := srv.Get(imageserver.Params{})
if err == nil {
t.Fatal("no error")
}
}
示例3: TestGetterErrorContextType
func TestGetterErrorContextType(t *testing.T) {
var data []byte
dest := groupcache.AllocatingByteSliceSink(&data)
gt := &Getter{
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return testdata.Medium, nil
}),
}
err := gt.Get("invalid", "foo", dest)
if err == nil {
t.Fatal("no error")
}
}
示例4: TestServerErrorServer
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")
}
}
示例5: TestGetterErrorSink
func TestGetterErrorSink(t *testing.T) {
ctx := &Context{
Params: imageserver.Params{},
}
dest := groupcache.AllocatingByteSliceSink(nil)
gt := &Getter{
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return testdata.Medium, nil
}),
}
err := gt.Get(ctx, "foo", dest)
if err == nil {
t.Fatal("no error")
}
}
示例6: TestGetterErrorServer
func TestGetterErrorServer(t *testing.T) {
ctx := &Context{
Params: imageserver.Params{},
}
var data []byte
dest := groupcache.AllocatingByteSliceSink(&data)
gt := &Getter{
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return nil, fmt.Errorf("error")
}),
}
err := gt.Get(ctx, "foo", dest)
if err == nil {
t.Fatal("no error")
}
}
示例7: TestServer
func TestServer(t *testing.T) {
srv := newTestServer(
imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return testdata.Medium, nil
}),
imageserver_cache.StringKeyGenerator("test"),
)
im, err := srv.Get(imageserver.Params{
imageserver.SourceParam: testdata.MediumFileName,
})
if err != nil {
t.Fatal(err)
}
if !imageserver.ImageEqual(im, testdata.Medium) {
t.Fatal("not equal")
}
}
示例8: TestGetterErrorImageMarshal
func TestGetterErrorImageMarshal(t *testing.T) {
ctx := &Context{
Params: imageserver.Params{},
}
var data []byte
dest := groupcache.AllocatingByteSliceSink(&data)
gt := &Getter{
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return &imageserver.Image{
Format: strings.Repeat("a", imageserver.ImageFormatMaxLen+1),
}, nil
}),
}
err := gt.Get(ctx, "foo", dest)
if err == nil {
t.Fatal("no error")
}
}
示例9: benchmarkServer
func benchmarkServer(b *testing.B, im *imageserver.Image, parallelism int) {
srv := newTestServer(
imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return im, nil
}),
imageserver_cache.StringKeyGenerator("test"),
)
params := imageserver.Params{}
b.SetParallelism(parallelism)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := srv.Get(params)
if err != nil {
b.Fatal(err)
}
}
})
b.SetBytes(int64(len(im.Data)))
}
示例10: TestServer
func TestServer(t *testing.T) {
srv := &Server{
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
if !params.Has(Param) {
t.Fatal("no source param")
}
if params.Has("foo") {
t.Fatal("unexpected param")
}
return &imageserver.Image{}, nil
}),
}
_, err := srv.Get(imageserver.Params{
Param: "source",
"foo": "bar",
})
if err != nil {
t.Fatal(err)
}
}
示例11: TestServerErrorCacheSet
func TestServerErrorCacheSet(t *testing.T) {
s := &Server{
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return testdata.Medium, nil
}),
Cache: &Func{
GetFunc: func(key string, params imageserver.Params) (*imageserver.Image, error) {
return nil, nil
},
SetFunc: func(key string, image *imageserver.Image, params imageserver.Params) error {
return fmt.Errorf("error")
},
},
KeyGenerator: StringKeyGenerator("test"),
}
_, err := s.Get(imageserver.Params{})
if err == nil {
t.Fatal("no error")
}
}
示例12: TestServer
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")
}
}
示例13: BenchmarkHandler
func BenchmarkHandler(b *testing.B) {
h := &Handler{
Parser: &nopParser{},
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return testdata.Medium, nil
}),
ETagFunc: func(params imageserver.Params) string {
return "foo"
},
}
rw := &nopResponseWriter{}
req, err := http.NewRequest("GET", "http://localhost", nil)
if err != nil {
b.Fatal(err)
}
req.Header.Set("If-None-Match", "\"bar\"")
b.ResetTimer()
for i := 0; i < b.N; i++ {
h.ServeHTTP(rw, req)
}
}
示例14: TestGetter
func TestGetter(t *testing.T) {
ctx := &Context{
Params: imageserver.Params{},
}
var data []byte
dest := groupcache.AllocatingByteSliceSink(&data)
gt := &Getter{
Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
return testdata.Medium, nil
}),
}
err := gt.Get(ctx, "foo", dest)
if err != nil {
t.Fatal(err)
}
im := new(imageserver.Image)
err = im.UnmarshalBinary(data)
if err != nil {
t.Fatal(err)
}
if !imageserver.ImageEqual(im, testdata.Medium) {
t.Fatal("not equal")
}
}
示例15: Get
// RandomFileName is the file name of Random.
RandomFileName = "random.png"
// Random is a random Image.
Random = loadImage(RandomFileName, "png")
// InvalidFileName is the file name of Invalid.
InvalidFileName = "invalid.jpg"
// Invalid is an invalid Image.
Invalid = loadImage(InvalidFileName, "invalid")
// Server is an Image Server that uses filename as source.
Server = imageserver.Server(imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) {
source, err := params.GetString(imageserver.SourceParam)
if err != nil {
return nil, err
}
im, err := Get(source)
if err != nil {
return nil, &imageserver.ParamError{Param: imageserver.SourceParam, Message: err.Error()}
}
return im, nil
}))
)
// Get returns an Image for a name.
func Get(name string) (*imageserver.Image, error) {
im, ok := Images[name]
if !ok {
return nil, fmt.Errorf("unknown image \"%s\"", name)
}
return im, nil
}