...

Source file src/github.com/coreos/go-systemd/v22/dbus/subscription_set_test.go

Documentation: github.com/coreos/go-systemd/v22/dbus

     1  // Copyright 2015 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dbus
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  // TestSubscribeUnit exercises the basics of subscription of a particular unit.
    23  func TestSubscriptionSetUnit(t *testing.T) {
    24  	target := "subscribe-events-set.service"
    25  
    26  	conn, err := New()
    27  
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	err = conn.Subscribe()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	subSet := conn.NewSubscriptionSet()
    38  	evChan, errChan := subSet.Subscribe()
    39  
    40  	subSet.Add(target)
    41  	setupUnit(target, conn, t)
    42  	linkUnit(target, conn, t)
    43  
    44  	reschan := make(chan string)
    45  	_, err = conn.StartUnit(target, "replace", reschan)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	job := <-reschan
    51  	if job != "done" {
    52  		t.Fatal("Couldn't start", target)
    53  	}
    54  
    55  	timeout := make(chan bool, 1)
    56  	go func() {
    57  		time.Sleep(3 * time.Second)
    58  		close(timeout)
    59  	}()
    60  
    61  	for {
    62  		select {
    63  		case changes := <-evChan:
    64  			tCh, ok := changes[target]
    65  
    66  			if !ok {
    67  				t.Fatal("Unexpected event:", changes)
    68  			}
    69  
    70  			if tCh.ActiveState == "active" && tCh.Name == target {
    71  				goto success
    72  			}
    73  		case err = <-errChan:
    74  			t.Fatal(err)
    75  		case <-timeout:
    76  			t.Fatal("Reached timeout")
    77  		}
    78  	}
    79  
    80  success:
    81  	return
    82  }
    83  

View as plain text