|
1 | | -package litestream_test |
| 1 | +package litestream |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "log" |
5 | | - "time" |
| 4 | + "slices" |
| 5 | + "strconv" |
| 6 | + "testing" |
6 | 7 |
|
7 | | - "github.com/benbjohnson/litestream/s3" |
8 | | - "github.com/ncruces/go-sqlite3/driver" |
| 8 | + "github.com/benbjohnson/litestream" |
9 | 9 | _ "github.com/ncruces/go-sqlite3/embed" |
10 | | - "github.com/ncruces/go-sqlite3/litestream" |
11 | 10 | ) |
12 | 11 |
|
13 | | -func ExampleNewReplica() { |
14 | | - client := s3.NewReplicaClient() |
15 | | - client.Bucket = "test-bucket" |
16 | | - client.Path = "fruits.db" |
17 | | - |
18 | | - litestream.NewReplica("fruits.db", client, litestream.ReplicaOptions{ |
19 | | - PollInterval: 5 * time.Second, |
20 | | - }) |
21 | | - |
22 | | - db, err := driver.Open("file:fruits.db?vfs=litestream") |
23 | | - if err != nil { |
24 | | - log.Fatalln(err) |
| 12 | +func Test_pollLevels(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + minLevel int |
| 15 | + want []int |
| 16 | + }{ |
| 17 | + {minLevel: -1, want: []int{0, 1, litestream.SnapshotLevel}}, |
| 18 | + {minLevel: 0, want: []int{0, 1, litestream.SnapshotLevel}}, |
| 19 | + {minLevel: 1, want: []int{1, litestream.SnapshotLevel}}, |
| 20 | + {minLevel: 2, want: []int{2, litestream.SnapshotLevel}}, |
| 21 | + {minLevel: 3, want: []int{3, litestream.SnapshotLevel}}, |
| 22 | + {minLevel: litestream.SnapshotLevel, want: []int{litestream.SnapshotLevel}}, |
| 23 | + {minLevel: litestream.SnapshotLevel + 1, want: []int{litestream.SnapshotLevel}}, |
25 | 24 | } |
26 | | - defer db.Close() |
27 | | - |
28 | | - for { |
29 | | - time.Sleep(time.Second) |
30 | | - rows, err := db.Query("SELECT * FROM fruits") |
31 | | - if err != nil { |
32 | | - log.Fatalln(err) |
33 | | - } |
34 | | - |
35 | | - for rows.Next() { |
36 | | - var name, color string |
37 | | - err := rows.Scan(&name, &color) |
38 | | - if err != nil { |
39 | | - log.Fatalln(err) |
| 25 | + for _, tt := range tests { |
| 26 | + t.Run(strconv.Itoa(tt.minLevel), func(t *testing.T) { |
| 27 | + got := pollLevels(tt.minLevel) |
| 28 | + if !slices.Equal(got, tt.want) { |
| 29 | + t.Errorf("pollLevels() = %v, want %v", got, tt.want) |
40 | 30 | } |
41 | | - log.Println(name, color) |
42 | | - } |
43 | | - |
44 | | - log.Println("===") |
45 | | - rows.Close() |
| 31 | + }) |
46 | 32 | } |
47 | 33 | } |
0 commit comments