gopkg.in/dedis/onet.v2@v2.0.0-20181115163211-c8f3724038a7/server_test.go (about)

     1  package onet
     2  
     3  import (
     4  	"testing"
     5  
     6  	bolt "github.com/coreos/bbolt"
     7  	"github.com/stretchr/testify/require"
     8  	"gopkg.in/dedis/onet.v2/log"
     9  	"gopkg.in/satori/go.uuid.v1"
    10  )
    11  
    12  func TestServer_ProtocolRegisterName(t *testing.T) {
    13  	c := NewLocalServer(tSuite, 0)
    14  	defer c.Close()
    15  	plen := len(c.protocols.instantiators)
    16  	require.True(t, plen > 0)
    17  	id, err := c.ProtocolRegister("ServerProtocol", NewServerProtocol)
    18  	log.ErrFatal(err)
    19  	require.NotNil(t, id)
    20  	require.True(t, plen < len(c.protocols.instantiators))
    21  	_, err = c.protocolInstantiate(ProtocolID(uuid.Nil), nil)
    22  	require.NotNil(t, err)
    23  	// Test for not overwriting
    24  	_, err = c.ProtocolRegister("ServerProtocol", NewServerProtocol2)
    25  	require.NotNil(t, err)
    26  }
    27  
    28  func TestServer_GetService(t *testing.T) {
    29  	c := NewLocalServer(tSuite, 0)
    30  	defer c.Close()
    31  	s := c.Service("nil")
    32  	require.Nil(t, s)
    33  }
    34  
    35  func TestServer_Database(t *testing.T) {
    36  	c := NewLocalServer(tSuite, 0)
    37  	require.NotNil(t, c.serviceManager.db)
    38  
    39  	for _, s := range c.serviceManager.availableServices() {
    40  		c.serviceManager.db.Update(func(tx *bolt.Tx) error {
    41  			b := tx.Bucket([]byte(s))
    42  			require.NotNil(t, b)
    43  			return nil
    44  		})
    45  	}
    46  	c.Close()
    47  }
    48  
    49  type ServerProtocol struct {
    50  	*TreeNodeInstance
    51  }
    52  
    53  // NewExampleHandlers initialises the structure for use in one round
    54  func NewServerProtocol(n *TreeNodeInstance) (ProtocolInstance, error) {
    55  	return &ServerProtocol{n}, nil
    56  }
    57  
    58  // NewExampleHandlers initialises the structure for use in one round
    59  func NewServerProtocol2(n *TreeNodeInstance) (ProtocolInstance, error) {
    60  	return &ServerProtocol{n}, nil
    61  }
    62  
    63  func (cp *ServerProtocol) Start() error {
    64  	return nil
    65  }