func newTestClientConnection() (*connector.IntegratedConnection, error) {
	var err error
	if testClientConnection != nil {
		return testClientConnection, nil
	}

	testClientConnection = connector.NewIntegratedConnection("Connector Test Framework", "newTestClientConnection")
	return testClientConnection, err
}
Example #2
0
func Example() {

	/* create a hub */
	hub, err := interfacehub.NewHub()
	if err != nil {
		log.Fatalf("Unable to create new hub: %s\n", err)
	}
	defer hub.Close()

	/* Done channel to shut stuff down*/
	Done := make(chan bool)

	/* create a client subscribe channel */
	cc := connector.NewIntegratedConnection("interfacehub example client", "Client Conn")
	cc.SetHub(hub)
	err = cc.Register()
	if err != nil {
		log.Fatalf("Unable to regoster client connection: %s\n", err)
	}

	/* Set up the filters for the client to listen on */
	csf := interfacehub.NewFilters(&ExampleServerOutput{})
	csf.Add(&filter.Equal{
		Description:     "Filter to accept specific messages",
		Field:           "Rejected",
		ActionOnMatch:   filter.FILTER_ACTION_DENY,
		ActionOnNoMatch: filter.FILTER_ACTION_CONTINUE,
		ExpectedValue:   true,
	})
	csf.Add(&filter.Equal{
		Description:     "Filter to reject empty messages and accept everything else",
		Field:           "UpperCaseMessage",
		ActionOnMatch:   filter.FILTER_ACTION_DENY,
		ActionOnNoMatch: filter.FILTER_ACTION_ACCEPT,
		ExpectedValue:   "",
	})

	/* set up a client to print the server output received */
	clientmsg := make(chan interface{}, 1)
	cc.SetSendDestination(clientmsg)

	/* have the client print out what it receives */
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		for {
			select {
			case <-Done:
				return
			case i := <-clientmsg:
				v, ok := i.(*ExampleServerOutput)
				if !ok {
					log.Fatal("Expected ExampleServerOutput message")
				}
				fmt.Printf("Echo from server: %s\n", v.UpperCaseMessage)
			}
		}
	}()

	/* do our client subscription to listen for the server response */
	err = cc.Subscribe(&ExampleServerOutput{}, csf)
	if err != nil {
		log.Fatalf("Could not subscribe client: %s\n", err)
	}

	/* create a server subscribe channel */
	sc := connector.NewIntegratedConnection("interfacehub example server", "Server Conn")
	sc.SetHub(hub)
	sc.Register()

	/* set up a server channel to listen  */
	servermsg := make(chan interface{}, 1)
	sc.SetSendDestination(servermsg)

	/* have the server reflect back in upper case */
	wg.Add(1)
	go func(conn *connector.IntegratedConnection) {
		defer wg.Done()
		for {
			select {
			case <-Done:
				return
			case i := <-servermsg:
				v, ok := i.(*ExampleServerInput)
				if !ok {
					log.Fatal("Expected ServerOutput message")
					return
				}
				conn.Publish(&ExampleServerOutput{
					Rejected:         v.Reject,
					UpperCaseMessage: strings.ToUpper(v.Message),
				})
			}
		}
	}(sc)

	/* do our server subscription to listen for the client requests */
	err = sc.Subscribe(&ExampleServerInput{}, nil)
	if err != nil {
		log.Fatalf("Could not subscribe server: %s\n", err)
	}

	/* Send the message*s from the client connection */

	msg := "My client message"
	fmt.Printf("Client Sending to server: %s\n", msg)
	err = cc.Publish(&ExampleServerInput{
		Message: msg,
		Reject:  false,
	})
	if err != nil {
		log.Fatalf("Could not publish to server: %s\n", err)
	}

	time.Sleep(1 * time.Millisecond)

	msg = "Another to be rejected by filter client message"
	fmt.Printf("Client Sending to server: %s\n", msg)
	err = cc.Publish(&ExampleServerInput{
		Message: msg,
		Reject:  true,
	})
	if err != nil {
		log.Fatalf("Could not publish to server: %s\n", err)
	}

	time.Sleep(1 * time.Millisecond)

	msg = "Last client message"
	fmt.Printf("Client Sending to server: %s\n", msg)
	err = cc.Publish(&ExampleServerInput{
		Message: msg,
		Reject:  false,
	})
	if err != nil {
		log.Fatalf("Could not publish to server: %s\n", err)
	}

	time.Sleep(1 * time.Millisecond)

	Done <- true
	Done <- true

	wg.Wait()

	// Output:
	// Client Sending to server: My client message
	// Echo from server: MY CLIENT MESSAGE
	// Client Sending to server: Another to be rejected by filter client message
	// Client Sending to server: Last client message
	// Echo from server: LAST CLIENT MESSAGE
}
func TestClientServerEcho(t *testing.T) {

	/* create a hub */
	hub, _ := interfacehub.NewHub()
	defer hub.Close()

	/* create a client subscribe channel */
	cc := connector.NewIntegratedConnection("interfacehub Unit Tests", "Client Conn")
	cc.SetHub(hub)
	cc.Register()

	/* create a server subscribe channel */
	sc := connector.NewIntegratedConnection("interfacehub Unit Tests", "Server Conn")
	sc.SetHub(hub)
	sc.Register()

	/* set up a client to print the server output received */
	pubmsg := make(chan interface{}, 1)
	cc.SetSendDestination(pubmsg)

	/* Set up the filters for the client to listen on */
	csf := interfacehub.NewFilters(&ServerOutput{})
	csf.Add(&filter.Equal{
		Description:     "Filter to accept specific messages",   // Rule description
		Field:           "UpperCaseMessage",                     // Interface field to match on
		ActionOnMatch:   filter.FILTER_ACTION_ACCEPT,            // Action on match
		ActionOnNoMatch: filter.FILTER_ACTION_CONTINUE,          // Action on not match
		ExpectedValue:   "MESSAGE WE ALWAYS WANT EVEN REJECTED", // Value interface field must match

	})

	csf.Add(&filter.Equal{
		Description:     "Filter to reject tagged messages",
		Field:           "Rejected",
		ActionOnMatch:   filter.FILTER_ACTION_ACCEPT,
		ActionOnNoMatch: filter.FILTER_ACTION_CONTINUE,
		ExpectedValue:   false,
	})
	csf.Add(&filter.Equal{
		Description:     "Everything we want to reject",
		Field:           "UpperCaseMessage",
		ActionOnMatch:   filter.FILTER_ACTION_DENY,
		ActionOnNoMatch: filter.FILTER_ACTION_DENY,
		ExpectedValue:   "",
	})

	/* do our client subscription to listen for the server response*/
	err := cc.Subscribe(&ServerOutput{}, csf)
	assert.NoError(t, err)

	err = sc.Publish(&ServerOutput{
		UpperCaseMessage: "MESSAGE WE ALWAYS WANT EVEN REJECTED",
		Rejected:         true,
	})
	assert.NoError(t, err)

	var i interface{}
	select {
	case i = <-pubmsg:
		v, ok := i.(*ServerOutput)
		assert.True(t, ok)
		assert.Equal(t, "MESSAGE WE ALWAYS WANT EVEN REJECTED", v.UpperCaseMessage)
		assert.True(t, v.Rejected)
	case <-time.After(10 * time.Millisecond):
		assert.Fail(t, "Timeout waiting for ServerOutput message")
	}

	err = sc.Publish(&ServerOutput{
		UpperCaseMessage: "ANOTHER MESSAGE WE WANT IF NOT REJECTED",
		Rejected:         true,
	})
	assert.NoError(t, err)

	select {
	case i = <-pubmsg:
		v, ok := i.(*ServerOutput)
		assert.True(t, ok)
		assert.Equal(t, "Nothing", v.UpperCaseMessage)
	case <-time.After(10 * time.Millisecond):
	}

	err = sc.Publish(&ServerOutput{
		UpperCaseMessage: "ANOTHER MESSAGE WE WANT IF NOT REJECTED",
		Rejected:         false,
	})
	assert.NoError(t, err)

	select {
	case i = <-pubmsg:
		v, ok := i.(*ServerOutput)
		assert.True(t, ok)
		assert.Equal(t, "ANOTHER MESSAGE WE WANT IF NOT REJECTED", v.UpperCaseMessage)
		assert.False(t, v.Rejected)
	case <-time.After(10 * time.Millisecond):
		assert.Fail(t, "Timeout waiting for ServerOutput message")
	}
}