github.com/tursom/GoCollections@v0.3.10/concurrent/collections/PublisherMessageQueue_test.go (about)

     1  /*
     2   * Copyright (c) 2022 tursom. All rights reserved.
     3   * Use of this source code is governed by a GPL-3
     4   * license that can be found in the LICENSE file.
     5   */
     6  
     7  package collections
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/tursom/GoCollections/util/time"
    14  
    15  	"github.com/tursom/GoCollections/unsafe"
    16  )
    17  
    18  func TestPublisherMessageQueueNode_Sizeof(t *testing.T) {
    19  	fmt.Println(unsafe.Sizeof[publisherMessageQueueNode[int]]())
    20  }
    21  
    22  func TestMessageQueue_Subscribe(t *testing.T) {
    23  	var mq PublisherMessageQueue[string]
    24  	for i := 0; i < 3; i++ {
    25  		id := i
    26  		subscribe := mq.Subscribe()
    27  		go func() {
    28  			fmt.Println("run subscriber", id)
    29  			defer func() {
    30  				subscribe.Close()
    31  				fmt.Println("subscriber", id, "closed")
    32  			}()
    33  
    34  			for true {
    35  				msg, _ := subscribe.Receive()
    36  				fmt.Println(id, msg)
    37  			}
    38  		}()
    39  	}
    40  	time.Sleep(time.Second / 10)
    41  
    42  	for i := 0; i < 3; i++ {
    43  		index := i
    44  		go func() {
    45  			for j := 0; j < 16; j++ {
    46  				mq.Send(fmt.Sprintf("%d-%d", index, j))
    47  			}
    48  		}()
    49  	}
    50  	time.Sleep(time.Second * 10)
    51  }