-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample_test.go
71 lines (61 loc) · 1.31 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package canvas_test
import (
"fmt"
"io"
"log"
"os"
"github.com/harrybrwn/go-canvas"
)
func Example_concurrentErrorHandling() {
var (
failed bool
err error
)
canvas.SetToken("bad token")
reset := canvas.ConcurrentErrorHandler
canvas.ConcurrentErrorHandler = func(e error) error {
if _, ok := e.(*canvas.Error); !ok {
failed = true
err = e
return e // non-nil will stop all goroutines
}
return nil // nil means we want to continue
}
count := 0
for file := range canvas.Files() {
if failed {
// log.Fatal(err)
break
}
if file != nil {
count++
}
}
canvas.ConcurrentErrorHandler = reset
fmt.Println(err)
fmt.Println(failed)
// Output:
// Invalid access token.
// true
canvas.SetToken(os.Getenv("CANVAS_TEST_TOKEN"))
}
func ExampleFile_AsWriteCloser() {
file := canvas.NewFile("test-file")
fmt.Println(file.ID == 0)
wc, err := file.AsWriteCloser()
if err != nil {
log.Fatal("could not create io.WriteCloser:", err)
}
if _, err = io.WriteString(wc, "this is a test file for the examples"); err != nil {
log.Fatal("could not write data:", err)
}
// close sends the data to canvas and updates the 'file' pointer
if err = wc.Close(); err != nil {
log.Fatal("could not send data: ", err)
}
fmt.Println(file.ID == 0)
// Output:
// true
// false
file.Delete()
}