add service type, action -- modify test for TakeAction

pull/334/head
Minh Doan 6 years ago committed by Minh Doan
parent f1245c117b
commit 350efc0bf9
  1. 1
      node/node.go
  2. 75
      node/service.go
  3. 4
      node/service_test.go

@ -140,6 +140,7 @@ type Node struct {
// Action Channel // Action Channel
actionChannel chan *Action actionChannel chan *Action
serviceStore *ServiceStore
// For test only // For test only
TestBankKeys []*ecdsa.PrivateKey TestBankKeys []*ecdsa.PrivateKey

@ -10,16 +10,42 @@ import (
// ActionType ... // ActionType ...
type ActionType byte type ActionType byte
// Constants ... // Constants for Action Type.
const ( const (
SyncingAction ActionType = iota Start ActionType = iota
KillSyncingAction Stop
)
// Type is service type.
type Type byte
// Constants for Type.
const (
SyncingSupport Type = iota
SupportClient SupportClient
SupportExplorer SupportExplorer
SyncingActionTest Test
Done Done
) )
func (t Type) String() string {
switch t {
case SupportClient:
return "SupportClient"
case SyncingSupport:
return "SyncingSupport"
case SupportExplorer:
return "SupportExplorer"
case Test:
return "Test"
case Done:
return "Done"
default:
return "Unknown"
}
}
// Constants for timing.
const ( const (
// WaitForStatusUpdate is the delay time to update new status. Currently set 1 second for development. Should be 30 minutes for production. // WaitForStatusUpdate is the delay time to update new status. Currently set 1 second for development. Should be 30 minutes for production.
WaitForStatusUpdate = time.Second * 1 WaitForStatusUpdate = time.Second * 1
@ -27,10 +53,22 @@ const (
// Action is type of service action. // Action is type of service action.
type Action struct { type Action struct {
t ActionType action ActionType
serviceType Type
params map[string]interface{} params map[string]interface{}
} }
// ServiceInterface ...
type ServiceInterface interface {
Start()
Stop()
}
// ServiceStore stores all services for service manager.
type ServiceStore struct {
services map[Type]ServiceInterface
}
// Start node. // Start node.
func (node *Node) Start() { func (node *Node) Start() {
node.actionChannel = node.StartServiceManager() node.actionChannel = node.StartServiceManager()
@ -43,18 +81,19 @@ func (node *Node) SendAction(action *Action) {
// TakeAction ... // TakeAction ...
func (node *Node) TakeAction(action *Action) { func (node *Node) TakeAction(action *Action) {
switch action.t { if node.serviceStore == nil {
case SyncingActionTest: utils.GetLogInstance().Error("Service store is not initialized.")
fmt.Println("Running syncing support") return
case SyncingAction: }
utils.GetLogInstance().Info("Running syncing support") if service, ok := node.serviceStore.services[action.serviceType]; ok {
go node.SupportSyncing() switch action.action {
case KillSyncingAction: case Start:
utils.GetLogInstance().Info("Killing syncing") fmt.Printf("Start %s\n", action.serviceType)
case SupportClient: service.Start()
go node.SupportClient() case Stop:
case SupportExplorer: fmt.Printf("Stop %s\n", action.serviceType)
go node.SupportExplorer() service.Stop()
}
} }
} }
@ -66,7 +105,7 @@ func (node *Node) StartServiceManager() chan *Action {
select { select {
case action := <-ch: case action := <-ch:
node.TakeAction(action) node.TakeAction(action)
if action.t == Done { if action.serviceType == Done {
return return
} }
case <-time.After(WaitForStatusUpdate): case <-time.After(WaitForStatusUpdate):

@ -12,9 +12,9 @@ func TestTakeAction(t *testing.T) {
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
select { select {
case <-time.After(WaitForStatusUpdate): case <-time.After(WaitForStatusUpdate):
node.SendAction(&Action{t: SyncingActionTest}) node.SendAction(&Action{action: Start, serviceType: SyncingSupport})
} }
} }
node.SendAction(&Action{t: Done}) node.SendAction(&Action{serviceType: Done})
} }

Loading…
Cancel
Save