forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_test.go
82 lines (66 loc) · 1.5 KB
/
utils_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
package contractcourt
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime/pprof"
"testing"
"time"
"github.com/lightningnetwork/lnd/channeldb"
)
// timeout implements a test level timeout.
func timeout() func() {
done := make(chan struct{})
go func() {
select {
case <-time.After(5 * time.Second):
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
panic("test timeout")
case <-done:
}
}()
return func() {
close(done)
}
}
func copyFile(dest, src string) error {
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
d, err := os.Create(dest)
if err != nil {
return err
}
if _, err := io.Copy(d, s); err != nil {
d.Close()
return err
}
return d.Close()
}
// copyChannelState copies the OpenChannel state by copying the database and
// creating a new struct from it. The copied state is returned.
func copyChannelState(t *testing.T, state *channeldb.OpenChannel) (
*channeldb.OpenChannel, error) {
// Make a copy of the DB.
dbFile := filepath.Join(state.Db.GetParentDB().Path(), "channel.db")
tempDbPath := t.TempDir()
tempDbFile := filepath.Join(tempDbPath, "channel.db")
err := copyFile(tempDbFile, dbFile)
if err != nil {
return nil, err
}
newDB := channeldb.OpenForTesting(t, tempDbPath)
chans, err := newDB.ChannelStateDB().FetchAllChannels()
if err != nil {
return nil, err
}
// We only support DBs with a single channel, for now.
if len(chans) != 1 {
return nil, fmt.Errorf("found %d chans in the db",
len(chans))
}
return chans[0], nil
}