本文整理匯總了Golang中camlistore/org/pkg/test.Fetcher.FetchErr方法的典型用法代碼示例。如果您正苦於以下問題:Golang Fetcher.FetchErr方法的具體用法?Golang Fetcher.FetchErr怎麽用?Golang Fetcher.FetchErr使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類camlistore/org/pkg/test.Fetcher
的用法示例。
在下文中一共展示了Fetcher.FetchErr方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestWriteThenRead
func TestWriteThenRead(t *testing.T) {
m := NewFileMap("test-file")
const size = 5 << 20
r := &randReader{seed: 123, length: size}
sto := new(test.Fetcher)
var buf bytes.Buffer
br, err := WriteFileMap(sto, m, io.TeeReader(r, &buf))
if err != nil {
t.Fatal(err)
}
var got bytes.Buffer
fr, err := NewFileReader(sto, br)
if err != nil {
t.Fatal(err)
}
n, err := io.Copy(&got, fr)
if err != nil {
t.Fatal(err)
}
if n != size {
t.Errorf("read back %d bytes; want %d", n, size)
}
if !bytes.Equal(buf.Bytes(), got.Bytes()) {
t.Error("bytes differ")
}
var offs []int
getOffsets := func() error {
offs = offs[:0]
var off int
return fr.ForeachChunk(func(_ []blob.Ref, p BytesPart) error {
offs = append(offs, off)
off += int(p.Size)
return err
})
}
if err := getOffsets(); err != nil {
t.Fatal(err)
}
sort.Ints(offs)
wantOffs := "[0 262144 358150 433428 525437 602690 675039 748088 816210 898743 980993 1053410 1120438 1188662 1265192 1332541 1398316 1463899 1530446 1596700 1668839 1738909 1817065 1891025 1961646 2031127 2099232 2170640 2238692 2304743 2374317 2440449 2514327 2582670 2653257 2753975 2827518 2905783 2975426 3053820 3134057 3204879 3271019 3346750 3421351 3487420 3557939 3624006 3701093 3768863 3842013 3918267 4001933 4069157 4139132 4208109 4281390 4348801 4422695 4490535 4568111 4642769 4709005 4785526 4866313 4933575 5005564 5071633 5152695 5227716]"
gotOffs := fmt.Sprintf("%v", offs)
if wantOffs != gotOffs {
t.Errorf("Got chunk offsets %v; want %v", gotOffs, wantOffs)
}
// Now force a fetch failure on one of the filereader schema chunks, to
// force a failure of GetChunkOffsets
errFetch := errors.New("fake fetch error")
var fetches struct {
sync.Mutex
n int
}
sto.FetchErr = func() error {
fetches.Lock()
defer fetches.Unlock()
fetches.n++
if fetches.n == 1 {
return nil
}
return errFetch
}
fr, err = NewFileReader(sto, br)
if err != nil {
t.Fatal(err)
}
if err := getOffsets(); fmt.Sprint(err) != "schema/filereader: fetching file schema blob: fake fetch error" {
t.Errorf("expected second call of GetChunkOffsets to return wrapped errFetch; got %v", err)
}
}