]> Sergey Matveev's repositories - btrtrc.git/blob - test/misc_test.go
Fix error unmarshalling bad metainfo nodes field
[btrtrc.git] / test / misc_test.go
1 package test
2
3 import (
4         "net"
5         "net/http"
6         "testing"
7
8         qt "github.com/frankban/quicktest"
9
10         "github.com/anacrolix/torrent"
11         "github.com/anacrolix/torrent/bencode"
12         "github.com/anacrolix/torrent/metainfo"
13         sqliteStorage "github.com/anacrolix/torrent/storage/sqlite"
14 )
15
16 func TestSqliteStorageClosed(t *testing.T) {
17         c := qt.New(t)
18         cfg := torrent.TestingConfig(t)
19         storage, err := sqliteStorage.NewDirectStorage(sqliteStorage.NewDirectStorageOpts{})
20         defer storage.Close()
21         cfg.DefaultStorage = storage
22         cfg.Debug = true
23         c.Assert(err, qt.IsNil)
24         cl, err := torrent.NewClient(cfg)
25         c.Assert(err, qt.IsNil)
26         defer cl.Close()
27         l, err := net.Listen("tcp", "localhost:0")
28         c.Assert(err, qt.IsNil)
29         defer l.Close()
30         // We need at least once piece to trigger a call to storage to determine completion state. We
31         // need non-zero content length to trigger piece hashing.
32         i := metainfo.Info{
33                 Pieces:      make([]byte, metainfo.HashSize),
34                 PieceLength: 1,
35                 Files: []metainfo.FileInfo{
36                         {Length: 1},
37                 },
38         }
39         mi := metainfo.MetaInfo{}
40         mi.InfoBytes, err = bencode.Marshal(i)
41         c.Assert(err, qt.IsNil)
42         s := http.Server{
43                 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44                         mi.Write(w)
45                 }),
46         }
47         defer s.Close()
48         go func() {
49                 err := s.Serve(l)
50                 if err != http.ErrServerClosed {
51                         panic(err)
52                 }
53         }()
54         // Close storage prematurely.
55         storage.Close()
56         tor, _, err := cl.AddTorrentSpec(&torrent.TorrentSpec{
57                 InfoHash: mi.HashInfoBytes(),
58                 Sources:  []string{"http://" + l.Addr().String()},
59         })
60         c.Assert(err, qt.IsNil)
61         <-tor.GotInfo()
62 }