| 2775 | } |
| 2776 | |
| 2777 | func TestHandleTopicTimeout(t *testing.T) { |
| 2778 | topicName := "usrMe" |
| 2779 | numUsers := 1 |
| 2780 | helper := TopicTestHelper{} |
| 2781 | helper.setUp(t, numUsers, types.TopicCatMe, topicName /*attach=*/, true) |
| 2782 | defer helper.tearDown() |
| 2783 | |
| 2784 | uid := helper.uids[0] |
| 2785 | helper.topic.perSubs = make(map[string]perSubsData) |
| 2786 | helper.topic.perSubs[uid.UserId()] = perSubsData{online: true} |
| 2787 | helper.hub.unreg = make(chan *topicUnreg, 10) |
| 2788 | uaTimer := time.NewTimer(time.Hour) |
| 2789 | notifTimer := time.NewTimer(time.Hour) |
| 2790 | helper.topic.handleTopicTimeout(helper.hub, "newUA", uaTimer, notifTimer) |
| 2791 | helper.finish() |
| 2792 | |
| 2793 | // Check for errors from testHubLoop |
| 2794 | if errorMsgs, hasError := helper.hubMessages["__ERROR__"]; hasError { |
| 2795 | t.Fatal(errorMsgs[0].Ctrl.Text) |
| 2796 | } |
| 2797 | |
| 2798 | if len(helper.hub.unreg) != 1 { |
| 2799 | t.Fatalf("Hub.unreg chan must contain exactly 1 message. Found %d.", len(helper.hub.unreg)) |
| 2800 | } |
| 2801 | if unreg := <-helper.hub.unreg; unreg.rcptTo != topicName { |
| 2802 | t.Errorf("unreg.rcptTo: expected '%s', found '%s'", topicName, unreg.rcptTo) |
| 2803 | } |
| 2804 | uaTimer.Stop() |
| 2805 | notifTimer.Stop() |
| 2806 | // Presence notifications. |
| 2807 | if len(helper.hubMessages) != 1 { |
| 2808 | t.Fatalf("Hub messages recipients: expected 1, received %d", len(helper.hubMessages)) |
| 2809 | } |
| 2810 | // Make sure uid's sessions are notified. |
| 2811 | if userPres, ok := helper.hubMessages[uid.UserId()]; ok { |
| 2812 | if len(userPres) != 1 { |
| 2813 | t.Fatalf("User presence messages: expected 1, got %d", len(userPres)) |
| 2814 | } |
| 2815 | pres := userPres[0].Pres |
| 2816 | if pres == nil { |
| 2817 | t.Fatal("Presence message expected in hub output, but not found.") |
| 2818 | } |
| 2819 | if pres.Topic != "me" { |
| 2820 | t.Errorf("Presence message topic: expected 'me', found '%s'", pres.Topic) |
| 2821 | } |
| 2822 | if pres.What != "off" { |
| 2823 | t.Errorf("Presence message what: expected 'off', found '%s'", pres.What) |
| 2824 | } |
| 2825 | if pres.Src != topicName { |
| 2826 | t.Errorf("Presence message src: expected '%s', found '%s'", topicName, pres.Src) |
| 2827 | } |
| 2828 | } else { |
| 2829 | t.Errorf("Hub expected to pres recipient %s", uid.UserId()) |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | func TestHandleTopicTermination(t *testing.T) { |
| 2834 | topicName := "usrMe" |