github.com/anycable/anycable-go@v1.5.1/node/node_mocks_test.go (about)

     1  package node
     2  
     3  import (
     4  	"log/slog"
     5  
     6  	"github.com/anycable/anycable-go/broker"
     7  	"github.com/anycable/anycable-go/common"
     8  	"github.com/anycable/anycable-go/encoders"
     9  	"github.com/anycable/anycable-go/metrics"
    10  	"github.com/anycable/anycable-go/mocks"
    11  	"github.com/anycable/anycable-go/pubsub"
    12  	"github.com/anycable/anycable-go/ws"
    13  )
    14  
    15  // NewMockNode build new node with mock controller
    16  func NewMockNode() *Node {
    17  	controller := mocks.NewMockController()
    18  	config := NewConfig()
    19  	config.HubGopoolSize = 2
    20  	node := NewNode(&config, WithInstrumenter(metrics.NewMetrics(nil, 10, slog.Default())), WithController(&controller))
    21  	node.SetBroker(broker.NewLegacyBroker(pubsub.NewLegacySubscriber(node)))
    22  	dconfig := NewDisconnectQueueConfig()
    23  	dconfig.Rate = 1
    24  	node.SetDisconnector(NewDisconnectQueue(node, &dconfig, slog.Default()))
    25  	return node
    26  }
    27  
    28  // NewMockSession returns a new session with a specified uid and identifiers equal to uid
    29  func NewMockSession(uid string, node *Node, opts ...SessionOption) *Session {
    30  	session := Session{
    31  		executor:      node,
    32  		closed:        true,
    33  		uid:           uid,
    34  		Log:           slog.With("sid", uid),
    35  		subscriptions: NewSubscriptionState(),
    36  		env:           common.NewSessionEnv("/cable-test", &map[string]string{}),
    37  		sendCh:        make(chan *ws.SentFrame, 256),
    38  		encoder:       encoders.JSON{},
    39  		metrics:       metrics.NoopMetrics{},
    40  	}
    41  
    42  	session.SetIdentifiers(uid)
    43  	session.conn = mocks.NewMockConnection()
    44  
    45  	for _, opt := range opts {
    46  		opt(&session)
    47  	}
    48  
    49  	go session.SendMessages()
    50  
    51  	return &session
    52  }
    53  
    54  // NewMockSession returns a new session with a specified uid, path and headers, and identifiers equal to uid
    55  func NewMockSessionWithEnv(uid string, node *Node, url string, headers *map[string]string, opts ...SessionOption) *Session {
    56  	session := NewMockSession(uid, node, opts...)
    57  	session.env = common.NewSessionEnv(url, headers)
    58  	return session
    59  }