當前位置: 首頁>>代碼示例>>Golang>>正文


Golang ServiceManager.State方法代碼示例

本文整理匯總了Golang中github.com/youtube/vitess/go/sync2.ServiceManager.State方法的典型用法代碼示例。如果您正苦於以下問題:Golang ServiceManager.State方法的具體用法?Golang ServiceManager.State怎麽用?Golang ServiceManager.State使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/youtube/vitess/go/sync2.ServiceManager的用法示例。


在下文中一共展示了ServiceManager.State方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestChannel

func TestChannel(t *testing.T) {
	logger := New("logger", 1)

	lastValue := sync2.AtomicString{}
	svm := sync2.ServiceManager{}
	svm.Go(func(_ *sync2.ServiceManager) {
		ch := logger.Subscribe(nil)
		defer logger.Unsubscribe(ch)
		for svm.State() == sync2.SERVICE_RUNNING {
			lastValue.Set(<-ch)
		}
	})

	time.Sleep(10 * time.Millisecond)
	if sz := logger.size.Get(); sz != 1 {
		t.Errorf("want 1, got %d", sz)
	}
	logger.Send(&logMessage{"val2"})
	time.Sleep(10 * time.Millisecond)
	if lastValue.Get() != "val2\n" {
		t.Errorf("want val2\\n, got %q", lastValue.Get())
	}

	go logger.Send(&logMessage{"val3"})
	svm.Stop()
	time.Sleep(10 * time.Millisecond)
	if sz := logger.size.Get(); sz != 0 {
		t.Errorf("want 0, got %d", sz)
	}
}
開發者ID:rn2dy,項目名稱:vitess,代碼行數:30,代碼來源:streamlog_test.go

示例2: TestFileInfo

func TestFileInfo(t *testing.T) {
	fname := path.Join(os.TempDir(), "binlog_streamer.test")
	writer, err := os.Create(fname)
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(fname)
	var file fileInfo
	err = file.Init(fname, 0)
	if err != nil {
		t.Fatal(err)
	}
	ch := make(chan []byte, 10)
	var svm = sync2.ServiceManager{}
	svm.Go(func(svc *sync2.ServiceContext) error {
		for svm.State() == sync2.SERVICE_RUNNING {
			file.WaitForChange(svc)
			b := make([]byte, 128)
			n, err := file.handle.Read(b)
			if err != nil {
				ch <- []byte(err.Error())
			}
			file.Set(file.lastPos + int64(n))
			ch <- b[:n]
		}
		return nil
	})

	want := "Message1"
	writer.WriteString(want)
	writer.Sync()
	got := string(<-ch)
	if want != got {
		t.Errorf("want %v, got %v", want, got)
	}

	want = "Message2"
	writer.WriteString(want)
	writer.Sync()
	got = string(<-ch)
	if want != got {
		t.Errorf("want %v, got %v", want, got)
	}

	time.Sleep(200 * time.Millisecond)
	want = "Message3"
	writer.WriteString(want)
	writer.Sync()
	got = string(<-ch)
	if want != got {
		t.Errorf("want %v, got %v", want, got)
	}

	want = "EOF"
	svm.Stop()
	got = string(<-ch)
	if want != got {
		t.Errorf("want %v, got %v", want, got)
	}
}
開發者ID:chinna1986,項目名稱:vitess,代碼行數:60,代碼來源:binlog_file_streamer_test.go

示例3: WaitForChange

func (f *fileInfo) WaitForChange(svm *sync2.ServiceManager) error {
	for {
		// Stop waiting if we're shutting down
		if svm.State() != sync2.SERVICE_RUNNING {
			return io.EOF
		}
		time.Sleep(100 * time.Millisecond)
		fi, err := f.handle.Stat()
		if err != nil {
			return fmt.Errorf("stat error: %v", err)
		}
		if fi.Size() != f.pos {
			return nil
		}
	}
}
開發者ID:qinbo,項目名稱:vitess,代碼行數:16,代碼來源:binlog_streamer.go

示例4: TestHTTP

func TestHTTP(t *testing.T) {
	l, err := net.Listen("tcp", ":0")
	if err != nil {
		t.Fatal(err)
	}
	addr := l.Addr().String()
	go http.Serve(l, nil)

	logger := New("logger", 1)
	logger.ServeLogs("/log")

	// This should not block
	logger.Send(&logMessage{"val1"})

	lastValue := sync2.AtomicString{}
	svm := sync2.ServiceManager{}
	svm.Go(func(_ *sync2.ServiceManager) {
		resp, err := http.Get(fmt.Sprintf("http://%s/log", addr))
		if err != nil {
			t.Fatal(err)
		}
		defer resp.Body.Close()
		buf := make([]byte, 100)
		for svm.State() == sync2.SERVICE_RUNNING {
			n, err := resp.Body.Read(buf)
			if err != nil {
				t.Fatal(err)
			}
			lastValue.Set(string(buf[:n]))
		}
	})

	time.Sleep(100 * time.Millisecond)
	if sz := logger.size.Get(); sz != 1 {
		t.Errorf("want 1, got %d", sz)
	}
	logger.Send(&logMessage{"val2"})
	time.Sleep(100 * time.Millisecond)
	if lastValue.Get() != "val2\n" {
		t.Errorf("want val2\\n, got %q", lastValue.Get())
	}

	// This part of the test is flaky.
	// Uncomment for one-time testing.
	/*
		// This send will unblock the http client
		// which will allow it to see the stop request
		// from svm.
		go logger.Send(&logMessage{"val3"})
		svm.Stop()
		// You have to send a few times before the writer
		// returns an error.
		for i := 0; i < 10; i++ {
			time.Sleep(100 * time.Millisecond)
			logger.Send(&logMessage{"val4"})
		}
		time.Sleep(100 * time.Millisecond)
		if sz := logger.size.Get(); sz != 0 {
			t.Errorf("want 0, got %d", sz)
		}
	*/
}
開發者ID:rn2dy,項目名稱:vitess,代碼行數:62,代碼來源:streamlog_test.go


注:本文中的github.com/youtube/vitess/go/sync2.ServiceManager.State方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。