~ubuntu-branches/ubuntu/trusty/golang-go-systemd/trusty-proposed

« back to all changes in this revision

Viewing changes to dbus/subscription_set_test.go

  • Committer: Package Import Robot
  • Author(s): Tianon Gravi
  • Date: 2014-05-08 09:48:52 UTC
  • Revision ID: package-import@ubuntu.com-20140508094852-0csgkii3h2wjlitj
Tags: upstream-1
ImportĀ upstreamĀ versionĀ 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package dbus
 
2
 
 
3
import (
 
4
        "testing"
 
5
        "time"
 
6
)
 
7
 
 
8
// TestSubscribeUnit exercises the basics of subscription of a particular unit.
 
9
func TestSubscriptionSetUnit(t *testing.T) {
 
10
        target := "subscribe-events-set.service"
 
11
 
 
12
        conn, err := New()
 
13
 
 
14
        if err != nil {
 
15
                t.Fatal(err)
 
16
        }
 
17
 
 
18
        err = conn.Subscribe()
 
19
        if err != nil {
 
20
                t.Fatal(err)
 
21
        }
 
22
 
 
23
        subSet := conn.NewSubscriptionSet()
 
24
        evChan, errChan := subSet.Subscribe()
 
25
 
 
26
        subSet.Add(target)
 
27
        setupUnit(target, conn, t)
 
28
 
 
29
        job, err := conn.StartUnit(target, "replace")
 
30
        if err != nil {
 
31
                t.Fatal(err)
 
32
        }
 
33
 
 
34
        if job != "done" {
 
35
                t.Fatal("Couldn't start", target)
 
36
        }
 
37
 
 
38
        timeout := make(chan bool, 1)
 
39
        go func() {
 
40
                time.Sleep(3 * time.Second)
 
41
                close(timeout)
 
42
        }()
 
43
 
 
44
        for {
 
45
                select {
 
46
                case changes := <-evChan:
 
47
                        tCh, ok := changes[target]
 
48
 
 
49
                        if !ok {
 
50
                                t.Fatal("Unexpected event %v", changes)
 
51
                        }
 
52
 
 
53
                        if tCh.ActiveState == "active" && tCh.Name == target {
 
54
                                goto success
 
55
                        }
 
56
                case err = <-errChan:
 
57
                        t.Fatal(err)
 
58
                case <-timeout:
 
59
                        t.Fatal("Reached timeout")
 
60
                }
 
61
        }
 
62
 
 
63
success:
 
64
        return
 
65
}
 
66
 
 
67