-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwatcher_test.go
112 lines (87 loc) · 3.13 KB
/
watcher_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package microstellar
import (
"context"
"fmt"
"log"
"time"
)
func ExampleMicroStellar_WatchPayments() {
// Create a new MicroStellar client connected to a fake network. To
// use a real network replace "fake" below with "test" or "public".
ms := New("fake")
// Watch for payments to address. (The fake network sends payments every 200ms.)
watcher, err := ms.WatchPayments("GCCRUJJGPYWKQWM5NLAXUCSBCJKO37VVJ74LIZ5AQUKT6KPVCPNAGC4A",
Opts().WithContext(context.Background()))
if err != nil {
log.Fatalf("Can't watch ledger: %+v", err)
}
// Count the number of payments received.
paymentsReceived := 0
go func() {
for p := range watcher.Ch {
paymentsReceived++
log.Printf("WatchPayments %d: %v -- %v %v from %v to %v\n", paymentsReceived, p.Type, p.Amount, p.AssetCode, p.From, p.To)
}
log.Printf("## WatchPayments ## Done -- Error: %v\n", *watcher.Err)
}()
// Stream the ledger for about a second then stop the watcher.
time.Sleep(1 * time.Second)
watcher.Done()
// Sleep a bit to wait for the done message from the goroutine.
time.Sleep(500 * time.Millisecond)
fmt.Printf("%d payments received", paymentsReceived)
// Output: 5 payments received
}
func ExampleMicroStellar_WatchTransactions() {
// Create a new MicroStellar client connected to a fake network. To
// use a real network replace "fake" below with "test" or "public".
ms := New("fake")
// Watch for transactions to address. (The fake network sends transactions every 200ms.)
watcher, err := ms.WatchTransactions("GCCRUJJGPYWKQWM5NLAXUCSBCJKO37VVJ74LIZ5AQUKT6KPVCPNAGC4A",
Opts().WithContext(context.Background()))
if err != nil {
log.Fatalf("Can't watch ledger: %+v", err)
}
// Count the number of transactions received.
received := 0
go func() {
for t := range watcher.Ch {
received++
log.Printf("WatchTransactions %d: %v %v %v\n", received, t.ID, t.Account, t.Ledger)
}
log.Printf("## WatchTransactions ## Done -- Error: %v\n", *watcher.Err)
}()
// Stream the ledger for about a second then stop the watcher.
time.Sleep(1 * time.Second)
watcher.Done()
// Sleep a bit to wait for the done message from the goroutine.
time.Sleep(500 * time.Millisecond)
fmt.Printf("%d transactions received", received)
// Output: 5 transactions received
}
func ExampleMicroStellar_WatchLedgers() {
// Create a new MicroStellar client connected to a fake network. To
// use a real network replace "fake" below with "test" or "public".
ms := New("fake")
// Get notified on new ledger entries in Stellar.
watcher, err := ms.WatchLedgers(Opts().WithCursor("now"))
if err != nil {
log.Fatalf("Can't watch ledger: %+v", err)
}
// Count the number of entries seen.
entries := 0
go func() {
for l := range watcher.Ch {
entries++
log.Printf("WatchLedgers %d: %v -- %v\n", entries, l.ID, l.TotalCoins)
}
log.Printf("## WatchLedgers ## Done -- Error: %v\n", *watcher.Err)
}()
// Stream the ledger for about a second then stop the watcher.
time.Sleep(1 * time.Second)
watcher.Done()
// Sleep a bit to wait for the done message from the goroutine.
time.Sleep(500 * time.Millisecond)
fmt.Printf("%d entries seen", entries)
// Output: 5 entries seen
}