mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-25 09:08:09 -07:00
initial middleware refactor
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
||||
// Use of this source code is governed by the BSD 2-Clause license,
|
||||
// which can be found in the LICENSE file.
|
||||
|
||||
// Package event implements type-level constraints for dealing with the events
|
||||
// communicated via BitTorrent announce.
|
||||
package event
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrUnknownEvent is returned when New fails to return an event.
|
||||
var ErrUnknownEvent = errors.New("unknown event")
|
||||
|
||||
// Event represents an event done by a BitTorrent client.
|
||||
type event uint8
|
||||
|
||||
const (
|
||||
// None is the event when a BitTorrent client announces due to time lapsed
|
||||
// since the previous announce.
|
||||
None event = iota
|
||||
|
||||
// Started is the event sent by a BitTorrent client when it joins a swarm.
|
||||
Started
|
||||
|
||||
// Stopped is the event sent by a BitTorrent client when it leaves a swarm.
|
||||
Stopped
|
||||
|
||||
// Completed is the event sent by a BitTorrent client when it finishes
|
||||
// downloading all of the required chunks.
|
||||
Completed
|
||||
)
|
||||
|
||||
var (
|
||||
eventToString = make(map[event]string)
|
||||
stringToEvent = make(map[string]event)
|
||||
)
|
||||
|
||||
func init() {
|
||||
eventToString[None] = "none"
|
||||
eventToString[Started] = "started"
|
||||
eventToString[Stopped] = "stopped"
|
||||
eventToString[Completed] = "completed"
|
||||
|
||||
stringToEvent[""] = None
|
||||
stringToEvent["none"] = None
|
||||
stringToEvent["started"] = Started
|
||||
stringToEvent["stopped"] = Stopped
|
||||
stringToEvent["completed"] = Completed
|
||||
}
|
||||
|
||||
// New returns the proper Event given a string.
|
||||
func New(eventStr string) (event, error) {
|
||||
if e, ok := stringToEvent[strings.ToLower(eventStr)]; ok {
|
||||
return e, nil
|
||||
}
|
||||
|
||||
return None, ErrUnknownEvent
|
||||
}
|
||||
|
||||
// String implements Stringer for an event.
|
||||
func (e event) String() string {
|
||||
if name, ok := eventToString[e]; ok {
|
||||
return name
|
||||
}
|
||||
|
||||
panic("event: event has no associated name")
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
||||
// Use of this source code is governed by the BSD 2-Clause license,
|
||||
// which can be found in the LICENSE file.
|
||||
|
||||
package event
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
var table = []struct {
|
||||
data string
|
||||
expected event
|
||||
expectedErr error
|
||||
}{
|
||||
{"", None, nil},
|
||||
{"NONE", None, nil},
|
||||
{"none", None, nil},
|
||||
{"started", Started, nil},
|
||||
{"stopped", Stopped, nil},
|
||||
{"completed", Completed, nil},
|
||||
{"notAnEvent", None, ErrUnknownEvent},
|
||||
}
|
||||
|
||||
for _, tt := range table {
|
||||
got, err := New(tt.data)
|
||||
assert.Equal(t, err, tt.expectedErr, "errors should equal the expected value")
|
||||
assert.Equal(t, got, tt.expected, "events should equal the expected value")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user