|
4 | 4 | "bufio"
|
5 | 5 | "crypto/tls"
|
6 | 6 | "fmt"
|
| 7 | + "io" |
7 | 8 | "log"
|
8 | 9 | "net/textproto"
|
9 | 10 | "reflect"
|
@@ -50,7 +51,7 @@ func TestCanConnectAndAuthenticate(t *testing.T) {
|
50 | 51 |
|
51 | 52 | for {
|
52 | 53 | message, err := tp.ReadLine()
|
53 |
| - if err != nil { |
| 54 | + if err != nil && err != io.EOF { |
54 | 55 | t.Fatal(err)
|
55 | 56 | }
|
56 | 57 | message = strings.Replace(message, "\r\n", "", 1)
|
@@ -82,6 +83,8 @@ func TestCanConnectAndAuthenticate(t *testing.T) {
|
82 | 83 | }
|
83 | 84 |
|
84 | 85 | func TestCanDisconnect(t *testing.T) {
|
| 86 | + keepAliveInterval = 9999 * time.Second |
| 87 | + |
85 | 88 | testMessage := "@badges=subscriber/6,premium/1;color=#FF0000;display-name=Redflamingo13;emotes=;id=2a31a9df-d6ff-4840-b211-a2547c7e656e;mod=0;room-id=11148817;subscriber=1;tmi-sent-ts=1490382457309;turbo=0;user-id=78424343;user-type= :redflamingo13!redflamingo13@redflamingo13.tmi.twitch.tv PRIVMSG #pajlada :Thrashh5, FeelsWayTooAmazingMan kinda"
|
86 | 89 | wait := make(chan struct{})
|
87 | 90 |
|
@@ -466,7 +469,7 @@ func TestCanSayMessage(t *testing.T) {
|
466 | 469 |
|
467 | 470 | for {
|
468 | 471 | message, err := tp.ReadLine()
|
469 |
| - if err != nil { |
| 472 | + if err != nil && err != io.EOF { |
470 | 473 | t.Fatal(err)
|
471 | 474 | }
|
472 | 475 | message = strings.Replace(message, "\r\n", "", 1)
|
@@ -536,7 +539,7 @@ func TestCanWhisperMessage(t *testing.T) {
|
536 | 539 |
|
537 | 540 | for {
|
538 | 541 | message, err := tp.ReadLine()
|
539 |
| - if err != nil { |
| 542 | + if err != nil && err != io.EOF { |
540 | 543 | t.Fatal(err)
|
541 | 544 | }
|
542 | 545 | message = strings.Replace(message, "\r\n", "", 1)
|
@@ -605,7 +608,7 @@ func TestCanJoinChannel(t *testing.T) {
|
605 | 608 |
|
606 | 609 | for {
|
607 | 610 | message, err := tp.ReadLine()
|
608 |
| - if err != nil { |
| 611 | + if err != nil && err != io.EOF { |
609 | 612 | t.Fatal(err)
|
610 | 613 | }
|
611 | 614 | message = strings.Replace(message, "\r\n", "", 1)
|
@@ -674,7 +677,7 @@ func TestCanPong(t *testing.T) {
|
674 | 677 |
|
675 | 678 | for {
|
676 | 679 | message, err := tp.ReadLine()
|
677 |
| - if err != nil { |
| 680 | + if err != nil && err != io.EOF { |
678 | 681 | t.Fatal(err)
|
679 | 682 | }
|
680 | 683 | message = strings.Replace(message, "\r\n", "", 1)
|
@@ -719,3 +722,117 @@ func TestCanNotDialInvalidAddress(t *testing.T) {
|
719 | 722 | t.Fatal("invalid Connect() error")
|
720 | 723 | }
|
721 | 724 | }
|
| 725 | + |
| 726 | +func TestCanCreateReconnectMessage(t *testing.T) { |
| 727 | + var reconnections int |
| 728 | + keepAliveInterval = 3 * time.Second |
| 729 | + testMessage := "@badges=subscriber/6,premium/1;color=#FF0000;display-name=Redflamingo13;emotes=;id=2a31a9df-d6ff-4840-b211-a2547c7e656e;mod=0;room-id=11148817;subscriber=1;tmi-sent-ts=1490382457309;turbo=0;user-id=78424343;user-type= :redflamingo13!redflamingo13@redflamingo13.tmi.twitch.tv PRIVMSG #pajlada :Thrashh5, FeelsWayTooAmazingMan kinda" |
| 730 | + wait := make(chan struct{}) |
| 731 | + |
| 732 | + go func() { |
| 733 | + cer, err := tls.LoadX509KeyPair("test_resources/server.crt", "test_resources/server.key") |
| 734 | + if err != nil { |
| 735 | + log.Println(err) |
| 736 | + return |
| 737 | + } |
| 738 | + config := &tls.Config{ |
| 739 | + Certificates: []tls.Certificate{cer}, |
| 740 | + } |
| 741 | + ln, err := tls.Listen("tcp", ":4331", config) |
| 742 | + if err != nil { |
| 743 | + t.Fatal(err) |
| 744 | + } |
| 745 | + close(wait) |
| 746 | + conn, err := ln.Accept() |
| 747 | + if err != nil { |
| 748 | + t.Fatal(err) |
| 749 | + } |
| 750 | + defer ln.Close() |
| 751 | + defer conn.Close() |
| 752 | + |
| 753 | + fmt.Fprintf(conn, "%s\r\n", testMessage) |
| 754 | + }() |
| 755 | + |
| 756 | + // wait for server to start |
| 757 | + select { |
| 758 | + case <-wait: |
| 759 | + case <-time.After(time.Second * 9): |
| 760 | + t.Fatal("server didn't start") |
| 761 | + } |
| 762 | + |
| 763 | + client := NewClient("justinfan123123", "oauth:123123132") |
| 764 | + client.IrcAddress = ":4331" |
| 765 | + |
| 766 | + client.OnNewReconnectMessage(func() { |
| 767 | + reconnections++ |
| 768 | + }) |
| 769 | + |
| 770 | + go client.Connect() |
| 771 | + |
| 772 | + waitMsg := make(chan string) |
| 773 | + |
| 774 | + // wait for server to start |
| 775 | + select { |
| 776 | + case <-waitMsg: |
| 777 | + case <-time.After(time.Second * 6): |
| 778 | + if reconnections >= 1 { |
| 779 | + t.Fatal("failed to a recieve onReconnectEvent event") |
| 780 | + } |
| 781 | + } |
| 782 | +} |
| 783 | + |
| 784 | +func TestCanReceiveReconnectMessage(t *testing.T) { |
| 785 | + var reconnections int |
| 786 | + testMessage := ":tmi.twitch.tv RECONNECT" |
| 787 | + wait := make(chan struct{}) |
| 788 | + |
| 789 | + go func() { |
| 790 | + cer, err := tls.LoadX509KeyPair("test_resources/server.crt", "test_resources/server.key") |
| 791 | + if err != nil { |
| 792 | + log.Println(err) |
| 793 | + return |
| 794 | + } |
| 795 | + config := &tls.Config{ |
| 796 | + Certificates: []tls.Certificate{cer}, |
| 797 | + } |
| 798 | + ln, err := tls.Listen("tcp", ":4332", config) |
| 799 | + if err != nil { |
| 800 | + t.Fatal(err) |
| 801 | + } |
| 802 | + close(wait) |
| 803 | + conn, err := ln.Accept() |
| 804 | + if err != nil { |
| 805 | + t.Fatal(err) |
| 806 | + } |
| 807 | + defer ln.Close() |
| 808 | + defer conn.Close() |
| 809 | + |
| 810 | + fmt.Fprintf(conn, "%s\r\n", testMessage) |
| 811 | + }() |
| 812 | + |
| 813 | + // wait for server to start |
| 814 | + select { |
| 815 | + case <-wait: |
| 816 | + case <-time.After(time.Second * 3): |
| 817 | + t.Fatal("server didn't start") |
| 818 | + } |
| 819 | + |
| 820 | + client := NewClient("justinfan123123", "oauth:123123132") |
| 821 | + client.IrcAddress = ":4332" |
| 822 | + go client.Connect() |
| 823 | + |
| 824 | + waitMsg := make(chan string) |
| 825 | + |
| 826 | + client.OnNewReconnectMessage(func() { |
| 827 | + reconnections++ |
| 828 | + close(waitMsg) |
| 829 | + }) |
| 830 | + |
| 831 | + // wait for server to start |
| 832 | + select { |
| 833 | + case <-waitMsg: |
| 834 | + case <-time.After(time.Second * 3): |
| 835 | + t.Fatal("no message sent") |
| 836 | + } |
| 837 | + assertIntsEqual(t, 1, reconnections) |
| 838 | +} |
0 commit comments