github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/client/pubsub_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestPublish(t *testing.T) {
     9  	if _, err := r.Publish("channel", "message"); err != nil {
    10  		t.Error(err)
    11  	}
    12  }
    13  
    14  func TestSubscribe(t *testing.T) {
    15  	quit := make(chan bool)
    16  	sub, err := r.PubSub()
    17  	if err != nil {
    18  		t.Error(err)
    19  	}
    20  	defer sub.Close()
    21  	go func() {
    22  		if err := sub.Subscribe("channel"); err != nil {
    23  			t.Error(err)
    24  			quit <- true
    25  			return
    26  		}
    27  		for {
    28  			list, err := sub.Receive()
    29  			if err != nil {
    30  				t.Error(err)
    31  				quit <- true
    32  				break
    33  			}
    34  			if list[0] == "message" {
    35  				if list[1] != "channel" || list[2] != "message" {
    36  					t.Fail()
    37  				}
    38  				quit <- true
    39  				break
    40  			}
    41  		}
    42  	}()
    43  	time.Sleep(100 * time.Millisecond)
    44  	r.Publish("channel", "message")
    45  	time.Sleep(100 * time.Millisecond)
    46  	<-quit
    47  }
    48  
    49  func TestPSubscribe(t *testing.T) {
    50  	quit := make(chan bool)
    51  	psub, err := r.PubSub()
    52  	if err != nil {
    53  		t.Error(err)
    54  	}
    55  	defer psub.Close()
    56  	go func() {
    57  		if err := psub.PSubscribe("news.*"); err != nil {
    58  			t.Error(err)
    59  			quit <- true
    60  			return
    61  		}
    62  		for {
    63  			list, err := psub.Receive()
    64  			if err != nil {
    65  				t.Error(err)
    66  				quit <- true
    67  				break
    68  			}
    69  			if list[0] == "pmessage" {
    70  				if list[1] != "news.*" || list[2] != "news.china" || list[3] != "message" {
    71  					t.Fail()
    72  				}
    73  				quit <- true
    74  				break
    75  			}
    76  		}
    77  	}()
    78  	time.Sleep(100 * time.Millisecond)
    79  	r.Publish("news.china", "message")
    80  	time.Sleep(100 * time.Millisecond)
    81  	<-quit
    82  }
    83  
    84  func TestUnSubscribe(t *testing.T) {
    85  	quit := false
    86  	ch := make(chan bool)
    87  	sub, err := r.PubSub()
    88  	if err != nil {
    89  		t.Error(err)
    90  	}
    91  	defer sub.Close()
    92  	go func() {
    93  		for {
    94  			if _, err := sub.Receive(); err != nil {
    95  				if !quit {
    96  					t.Error(err)
    97  				}
    98  			}
    99  			ch <- true
   100  		}
   101  	}()
   102  	time.Sleep(100 * time.Millisecond)
   103  	sub.Subscribe("channel")
   104  	time.Sleep(100 * time.Millisecond)
   105  	<-ch
   106  	if len(sub.Channels) != 1 {
   107  		t.Fail()
   108  	}
   109  	if err := sub.UnSubscribe("channel"); err != nil {
   110  		t.Error(err)
   111  	}
   112  	time.Sleep(100 * time.Millisecond)
   113  	<-ch
   114  	time.Sleep(100 * time.Millisecond)
   115  	if len(sub.Channels) != 0 {
   116  		t.Fail()
   117  	}
   118  	quit = true
   119  }
   120  
   121  func TestPUnSubscribe(t *testing.T) {
   122  	quit := false
   123  	ch := make(chan bool)
   124  	sub, err := r.PubSub()
   125  	if err != nil {
   126  		t.Error(err)
   127  	}
   128  	defer sub.Close()
   129  	go func() {
   130  		for {
   131  			if _, err := sub.Receive(); err != nil {
   132  				if !quit {
   133  					t.Error(err)
   134  				}
   135  			}
   136  			ch <- true
   137  		}
   138  	}()
   139  	time.Sleep(100 * time.Millisecond)
   140  	sub.PSubscribe("news.*")
   141  	time.Sleep(100 * time.Millisecond)
   142  	<-ch
   143  	if len(sub.Patterns) != 1 {
   144  		t.Fail()
   145  	}
   146  	if err := sub.PUnSubscribe("news.*"); err != nil {
   147  		t.Error(err)
   148  	}
   149  	time.Sleep(100 * time.Millisecond)
   150  	<-ch
   151  	time.Sleep(100 * time.Millisecond)
   152  	if len(sub.Patterns) != 0 {
   153  		t.Fail()
   154  	}
   155  	quit = true
   156  }