-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from yowcow/dev-handler-multiplexer
multiplex storage
- Loading branch information
Showing
5 changed files
with
199 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package handler | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type handlerMap map[string]Handler | ||
type nsHandlerMap map[string]NSHandler | ||
|
||
type Multiplexer struct { | ||
handlers handlerMap | ||
nshandlers nsHandlerMap | ||
} | ||
|
||
func NewMultiplexer() *Multiplexer { | ||
return &Multiplexer{ | ||
handlerMap{}, | ||
nsHandlerMap{}, | ||
} | ||
} | ||
|
||
func (m *Multiplexer) RegisterHandler(name string, hdr Handler) error { | ||
if _, ok := m.handlers[name]; ok { | ||
return fmt.Errorf("handler with name '%s' already registered", name) | ||
} | ||
m.handlers[name] = hdr | ||
return nil | ||
} | ||
|
||
func (m *Multiplexer) RegisterNSHandler(name string, hdr NSHandler) error { | ||
if _, ok := m.nshandlers[name]; ok { | ||
return fmt.Errorf("nshandler with name '%s' already registered", name) | ||
} | ||
m.nshandlers[name] = hdr | ||
return nil | ||
} | ||
|
||
func (m Multiplexer) GetHandler(name string) (Handler, error) { | ||
if hdr, ok := m.handlers[name]; ok { | ||
return hdr, nil | ||
} | ||
return nil, fmt.Errorf("handler with name '%s' not registered", name) | ||
} | ||
|
||
func (m Multiplexer) GetNSHandler(name string) (NSHandler, error) { | ||
if hdr, ok := m.nshandlers[name]; ok { | ||
return hdr, nil | ||
} | ||
return nil, fmt.Errorf("nshandler with name '%s' not registered", name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/yowcow/goromdb/loader" | ||
) | ||
|
||
var ( | ||
_ Handler = (*testHandler)(nil) | ||
_ NSHandler = (*testNSHandler)(nil) | ||
) | ||
|
||
type testHandler struct { | ||
name string | ||
} | ||
|
||
func (h *testHandler) Get(k []byte) ([]byte, error) { | ||
return []byte(fmt.Sprintf("get %s from %s", string(k), h.name)), nil | ||
} | ||
|
||
func (h *testHandler) Load(file string) error { | ||
return nil | ||
} | ||
|
||
func (h *testHandler) Start(filein <-chan string, ldr *loader.Loader) <-chan bool { | ||
return nil | ||
} | ||
|
||
type testNSHandler struct { | ||
testHandler | ||
} | ||
|
||
func (h *testNSHandler) GetNS(ns, k []byte) ([]byte, error) { | ||
return []byte(fmt.Sprintf("get %s in ns %s from %s", string(k), string(ns), h.name)), nil | ||
} | ||
|
||
func TestRegisterHandler(t *testing.T) { | ||
m := NewMultiplexer() | ||
|
||
err := m.RegisterHandler("h1", new(testHandler)) | ||
|
||
assert.Nil(t, err) | ||
|
||
err = m.RegisterHandler("h1", new(testHandler)) | ||
|
||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestRegisterNSHandler(t *testing.T) { | ||
m := NewMultiplexer() | ||
|
||
err := m.RegisterNSHandler("h1", new(testNSHandler)) | ||
|
||
assert.Nil(t, err) | ||
|
||
err = m.RegisterNSHandler("h1", new(testNSHandler)) | ||
|
||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestGetHandler(t *testing.T) { | ||
m := NewMultiplexer() | ||
_ = m.RegisterHandler("h1", &testHandler{"h1"}) | ||
hdr, err := m.GetHandler("h1") | ||
|
||
assert.NotNil(t, hdr) | ||
assert.Nil(t, err) | ||
|
||
hdr, err = m.GetHandler("h2") | ||
|
||
assert.Nil(t, hdr) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestGetNSHandler(t *testing.T) { | ||
m := NewMultiplexer() | ||
_ = m.RegisterNSHandler("h1", &testNSHandler{testHandler{"h1"}}) | ||
hdr, err := m.GetNSHandler("h1") | ||
|
||
assert.NotNil(t, hdr) | ||
assert.Nil(t, err) | ||
|
||
hdr, err = m.GetNSHandler("h2") | ||
|
||
assert.Nil(t, hdr) | ||
assert.NotNil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.