]> Sergey Matveev's repositories - btrtrc.git/blob - test/misc_test.go
a72746c8eb055eb3a9ff31e552cd0c1df6b2c470
[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 TestUseSourcesSqliteStorageClosed(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         c.Assert(err, qt.IsNil)
23         cl, err := torrent.NewClient(cfg)
24         c.Assert(err, qt.IsNil)
25         defer cl.Close()
26         l, err := net.Listen("tcp", "localhost:0")
27         c.Assert(err, qt.IsNil)
28         defer l.Close()
29         // We need at least once piece to trigger a call to storage to determine completion state.
30         i := metainfo.Info{Pieces: make([]byte, metainfo.HashSize)}
31         mi := metainfo.MetaInfo{}
32         mi.InfoBytes, err = bencode.Marshal(i)
33         c.Assert(err, qt.IsNil)
34         s := http.Server{
35                 Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
36                         mi.Write(w)
37                 }),
38         }
39         defer s.Close()
40         go func() {
41                 err := s.Serve(l)
42                 if err != http.ErrServerClosed {
43                         panic(err)
44                 }
45         }()
46         // Close storage prematurely.
47         storage.Close()
48         tor, _, err := cl.AddTorrentSpec(&torrent.TorrentSpec{
49                 InfoHash: mi.HashInfoBytes(),
50                 Sources:  []string{"http://" + l.Addr().String()},
51         })
52         c.Assert(err, qt.IsNil)
53         <-tor.GotInfo()
54 }