removal of stop printer channel

This commit is contained in:
ap-pauloafonso 2021-03-22 22:09:38 -03:00
parent 6cb7064828
commit c94fd60f77
4 changed files with 67 additions and 16 deletions

View file

@ -13,14 +13,8 @@ import (
)
func PrintState(state *ratiospoof.RatioSpoof) {
exit := false
go func() {
_ = <-state.StopPrintCH
exit = true
}()
for {
if exit {
if !state.Print {
break
}
width := terminalSize()

View file

@ -34,7 +34,7 @@ type RatioSpoof struct {
AnnounceCount int
Status string
AnnounceHistory announceHistory
StopPrintCH chan interface{}
Print bool
}
type AnnounceEntry struct {
@ -50,7 +50,6 @@ type announceHistory struct {
}
func NewRatioSpoofState(input input.InputArgs) (*RatioSpoof, error) {
stopPrintCh := make(chan interface{})
dat, err := os.ReadFile(input.TorrentPath)
if err != nil {
return nil, err
@ -83,7 +82,7 @@ func NewRatioSpoofState(input input.InputArgs) (*RatioSpoof, error) {
Input: inputParsed,
NumWant: 200,
Status: "started",
StopPrintCH: stopPrintCh,
Print: true,
}, nil
}
@ -117,7 +116,7 @@ func (r *RatioSpoof) Run() {
}
}()
<-sigCh
r.StopPrintCH <- "exit print"
r.Print = false
r.gracefullyExit()
}
func (r *RatioSpoof) firstAnnounce() {

View file

@ -41,7 +41,7 @@ func NewHttpTracker(torrentInfo *bencode.TorrentInfo) (*HttpTracker, error) {
return &HttpTracker{Urls: torrentInfo.TrackerInfo.Urls}, nil
}
func (t *HttpTracker) SwapFirst(currentIdx int) {
func (t *HttpTracker) swapFirst(currentIdx int) {
aux := t.Urls[0]
t.Urls[0] = t.Urls[currentIdx]
t.Urls[currentIdx] = aux
@ -50,7 +50,7 @@ func (t *HttpTracker) SwapFirst(currentIdx int) {
func (t *HttpTracker) updateEstimatedTimeToAnnounce(interval int) {
t.EstimatedTimeToAnnounce = time.Now().Add(time.Duration(interval) * time.Second)
}
func (t *HttpTracker) HandleSuccessfulResponse(resp *TrackerResponse) {
func (t *HttpTracker) handleSuccessfulResponse(resp *TrackerResponse) {
if resp.Interval <= 0 {
resp.Interval = 1800
}
@ -76,7 +76,7 @@ func (t *HttpTracker) Announce(query string, headers map[string]string, retry bo
}
continue
}
t.HandleSuccessfulResponse(trackerResp)
t.handleSuccessfulResponse(trackerResp)
return trackerResp, nil
}
@ -85,7 +85,7 @@ func (t *HttpTracker) Announce(query string, headers map[string]string, retry bo
if err != nil {
return nil, err
}
t.HandleSuccessfulResponse(resp)
t.handleSuccessfulResponse(resp)
return resp, nil
}
}
@ -121,7 +121,7 @@ func (t *HttpTracker) tryMakeRequest(query string, headers map[string]string) (*
continue
}
if idx != 0 {
t.SwapFirst(idx)
t.swapFirst(idx)
}
return &ret, nil

View file

@ -0,0 +1,58 @@
package tracker
import (
"reflect"
"testing"
"github.com/ap-pauloafonso/ratio-spoof/internal/bencode"
)
func TestNewHttpTracker(t *testing.T) {
_, err := NewHttpTracker(&bencode.TorrentInfo{TrackerInfo: &bencode.TrackerInfo{Urls: []string{"udp://url1", "udp://url2"}}})
got := err.Error()
want := "No tcp/http tracker url announce found"
if got != want {
t.Errorf("got: %v want %v", got, want)
}
}
func TestSwapFirst(t *testing.T) {
tracker, _ := NewHttpTracker(&bencode.TorrentInfo{TrackerInfo: &bencode.TrackerInfo{Urls: []string{"http://url1", "http://url2", "http://url3", "http://url4"}}})
tracker.swapFirst(3)
got := tracker.Urls
want := []string{"http://url4", "http://url2", "http://url3", "http://url1"}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v want %v", got, want)
}
}
func TestHandleSuccessfulResponse(t *testing.T) {
t.Run("Empty interval should be overided with 1800 ", func(t *testing.T) {
tracker, _ := NewHttpTracker(&bencode.TorrentInfo{TrackerInfo: &bencode.TrackerInfo{Urls: []string{"http://url1", "http://url2", "http://url3", "http://url4"}}})
r := TrackerResponse{}
tracker.handleSuccessfulResponse(&r)
got := r.Interval
want := 1800
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v want %v", got, want)
}
})
t.Run("Valid interval shouldn't be overwritten", func(t *testing.T) {
tracker, _ := NewHttpTracker(&bencode.TorrentInfo{TrackerInfo: &bencode.TrackerInfo{Urls: []string{"http://url1", "http://url2", "http://url3", "http://url4"}}})
r := TrackerResponse{Interval: 900}
tracker.handleSuccessfulResponse(&r)
got := r.Interval
want := 900
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v want %v", got, want)
}
})
}