Commit ff515999 authored by Geoff Simmons's avatar Geoff Simmons

Refactor and simplify vsm.Attach().

Attach() takes one argument for the instance, default for "", otherwise
the named instance.

Remove AttachInstance() and the progress argument.
parent 55b50754
......@@ -84,28 +84,20 @@ func TestError(t *testing.T) {
func TestAttach(t *testing.T) {
v := New()
defer v.Destroy()
if err := v.Attach(false); err != nil {
t.Fatal("Attach(false):", err)
if err := v.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
var n *VSM
if err := n.Attach(false); err == nil {
if err := n.Attach(""); err == nil {
t.Error("expected nil.Attach() to fail")
}
uninit := new(VSM)
if err := uninit.Attach(false); err == nil {
if err := uninit.Attach(""); err == nil {
t.Error("expected uninitialized.Attach() to fail")
}
}
func TestAttachProgress(t *testing.T) {
v := New()
defer v.Destroy()
if err := v.Attach(true); err != nil {
t.Error("Attach(true):", err)
}
}
const zerosec = time.Duration(0)
func TestAttachTmo(t *testing.T) {
......@@ -130,8 +122,8 @@ func TestAttachTmo(t *testing.T) {
func TestAttachInstance(t *testing.T) {
v := New()
defer v.Destroy()
if err := v.AttachInstance("gotest", false); err != nil {
t.Error("AttachInstance(\"gotest\"):", err)
if err := v.Attach("gotest"); err != nil {
t.Error("Attach(\"gotest\"):", err)
}
f := New()
......@@ -139,19 +131,10 @@ func TestAttachInstance(t *testing.T) {
if err := f.AttachTmo(zerosec); err != nil {
t.Error("AttachTmo(0s):", err)
}
if err := f.AttachInstance("instanceDoesNotExist", false); err == nil {
t.Error("expected AttachInstance() to fail for a " +
if err := f.Attach("instanceDoesNotExist"); err == nil {
t.Error("expected Attach() to fail for a " +
"non-existent instance")
}
var n *VSM
if err := n.AttachInstance("gotest", false); err == nil {
t.Error("expected nil.AttachInstance() to fail")
}
uninit := new(VSM)
if err := uninit.AttachInstance("gotest", false); err == nil {
t.Error("expected uninitialized.AttachInstance() to fail")
}
}
func TestPointer(t *testing.T) {
......
......@@ -115,44 +115,27 @@ func (v *VSM) AttachTmo(tmo time.Duration) error {
return nil
}
func (v *VSM) attach(progress bool) error {
prog := -1
if progress {
prog = 0
}
C.VSM_ResetError(v.vsm)
if C.VSM_Attach(v.vsm, C.int(prog)) != 0 {
return v
}
return nil
}
// Attach to the default instance of Varnish -- the instance of
// varnishd invoked without the -n option.
// Attach to an instance of Varnish -- varnishd with a running worker
// process.
//
// If the bool argument is true, a dot ('.') is printed to standard
// output for each second waiting for the attach to succeed. If the
// attach fails after timeout, a newline is printed. If the argument
// is false, there is no such output.
func (v *VSM) Attach(progress bool) error {
// If name != "", then attach to the default instance -- varnishd
// invoked without the -n option. Otherwise, attach to the named
// instance, as given in the -n option.
func (v *VSM) Attach(name string) error {
if err := v.checkNil(); err != nil {
return err
}
return v.attach(progress)
}
// AttachInstance attaches to a named instance of Varnish, where the
// string corresponds to the -n argument in the invocation of
// varnishd.
func (v *VSM) AttachInstance(inst string, progress bool) error {
if err := v.checkNil(); err != nil {
return err
if name != "" {
C.VSM_ResetError(v.vsm)
if C.VSM_Arg(v.vsm, 'n', C.CString(name)) != 1 {
return v
}
}
C.VSM_ResetError(v.vsm)
if C.VSM_Arg(v.vsm, 'n', C.CString(inst)) != 1 {
if C.VSM_Attach(v.vsm, C.int(-1)) != 0 {
return v
}
return v.attach(progress)
return nil
}
// Pointer returns an internal pointer for use by other packages.
......
......@@ -45,28 +45,20 @@ import (
func TestAttach(t *testing.T) {
l := New()
defer l.Release()
if err := l.Attach(false); err != nil {
t.Fatal("Attach(false):", err)
if err := l.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
var n *Log
if err := n.Attach(false); err == nil {
if err := n.Attach(""); err == nil {
t.Error("expected nil.Attach() to fail")
}
uninit := new(Log)
if err := uninit.Attach(false); err == nil {
if err := uninit.Attach(""); err == nil {
t.Error("expected uninitialized.Attach() to fail")
}
}
func TestAttachProgress(t *testing.T) {
l := New()
defer l.Release()
if err := l.Attach(true); err != nil {
t.Error("Attach(true):", err)
}
}
const zerosec = time.Duration(0)
func TestAttachTmo(t *testing.T) {
......@@ -91,7 +83,7 @@ func TestAttachTmo(t *testing.T) {
func TestAttachInstance(t *testing.T) {
l := New()
defer l.Release()
if err := l.AttachInstance("gotest", false); err != nil {
if err := l.Attach("gotest"); err != nil {
t.Error("AttachInstance(\"gotest\"):", err)
}
......@@ -100,19 +92,10 @@ func TestAttachInstance(t *testing.T) {
if err := f.AttachTmo(zerosec); err != nil {
t.Error("AttachTmo(0s):", err)
}
if err := f.AttachInstance("instanceDoesNotExist", false); err == nil {
t.Error("expected AttachInstance() to fail for a " +
if err := f.Attach("instanceDoesNotExist"); err == nil {
t.Error("expected Attach() to fail for a " +
"non-existent instance")
}
var n *Log
if err := n.AttachInstance("gotest", false); err == nil {
t.Error("expected nil.AttachInstance() to fail")
}
uninit := new(Log)
if err := uninit.AttachInstance("gotest", false); err == nil {
t.Error("expected uninitialized.AttachInstance() to fail")
}
}
var tx2recType = map[TxType]RecordType{
......@@ -445,8 +428,8 @@ func url(path string) string {
func TestE2EDefaultRead(t *testing.T) {
l := New()
defer l.Release()
if err := l.Attach(false); err != nil {
t.Fatal("Attach(false):", err)
if err := l.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
statusChan := make(chan Status)
......
......@@ -470,35 +470,17 @@ func (log *Log) setCursor() error {
return nil
}
// Attach to the default instance of Varnish -- the instance of
// varnishd invoked without the -n option.
// Attach to an instance of Varnish -- varnishd with a running worker
// process.
//
// If progress is true, a dot ('.') is printed to standard output for
// each second waiting for the attach to succeed. If the attach fails
// after timeout, a newline is printed. If progress is false, there is
// no such output.
func (log *Log) Attach(progress bool) error {
// If name != "", then attach to the default instance -- varnishd
// invoked without the -n option. Otherwise, attach to the named
// instance, as given in the -n option.
func (log *Log) Attach(name string) error {
if err := log.initVSM(); err != nil {
return err
}
if err := log.vsm.Attach(progress); err != nil {
return err
}
if err := log.setCursor(); err != nil {
return nil
}
return nil
}
// AttachInstance attaches to a named instance of Varnish, where name
// corresponds to the -n argument in the invocation of varnishd.
//
// The progress argument has the same meaning as for Attach.
func (log *Log) AttachInstance(name string, progress bool) error {
if err := log.initVSM(); err != nil {
return err
}
if err := log.vsm.AttachInstance(name, progress); err != nil {
if err := log.vsm.Attach(name); err != nil {
return err
}
if err := log.setCursor(); err != nil {
......
......@@ -106,16 +106,16 @@ func TestNewRelease(t *testing.T) {
func TestAttach(t *testing.T) {
s := New()
defer s.Release()
if err := s.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
var n *Stats
if err := n.Attach(); err == nil {
if err := n.Attach(""); err == nil {
t.Error("expected nil.Attach() to fail")
}
uninit := new(Stats)
if err := uninit.Attach(); err == nil {
if err := uninit.Attach(""); err == nil {
t.Error("expected uninitialized.Attach() to fail")
}
}
......@@ -142,8 +142,8 @@ func TestAttachTmo(t *testing.T) {
func TestAttachInstance(t *testing.T) {
s := New()
defer s.Release()
if err := s.AttachInstance("gotest"); err != nil {
t.Error("AttachInstance(\"gotest\"):", err)
if err := s.Attach("gotest"); err != nil {
t.Error("Attach(\"gotest\"):", err)
}
f := New()
......@@ -151,19 +151,10 @@ func TestAttachInstance(t *testing.T) {
if err := f.AttachTmo(zerosec); err != nil {
t.Error("AttachTmo(0s):", err)
}
if err := f.AttachInstance("instanceDoesNotExist"); err == nil {
t.Error("expected AttachInstance() to fail for a " +
if err := f.Attach("instanceDoesNotExist"); err == nil {
t.Error("expected Attach() to fail for a " +
"non-existent instance")
}
var n *Stats
if err := n.AttachInstance("gotest"); err == nil {
t.Error("expected nil.AttachInstance() to fail")
}
uninit := new(Stats)
if err := uninit.AttachInstance("gotest"); err == nil {
t.Error("expected uninitialized.AttachInstance() to fail")
}
}
func TestLevels(t *testing.T) {
......@@ -191,8 +182,8 @@ func TestLevels(t *testing.T) {
func TestD9ns(t *testing.T) {
s := New()
defer s.Release()
if err := s.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
for _, v := range expStats {
......@@ -270,8 +261,8 @@ func TestD9ns(t *testing.T) {
func TestNames(t *testing.T) {
s := New()
defer s.Release()
if err := s.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
names, err := s.Names()
if err != nil {
......@@ -326,8 +317,8 @@ func TestInclude(t *testing.T) {
t.Fatal("Include(MGT.*):", err)
return
}
if err := s.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
return
}
names, err := s.Names()
......@@ -352,8 +343,8 @@ func TestInclude(t *testing.T) {
t.Fatal("Include(MGT.uptime):", err)
return
}
if err := s2.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s2.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
return
}
names, err = s2.Names()
......@@ -382,8 +373,8 @@ func TestInclude(t *testing.T) {
}
attached := New()
defer attached.Release()
if err := attached.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := attached.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
if err := attached.Include(fld); err == nil {
t.Error("expected Include() to fail for attached instance")
......@@ -397,8 +388,8 @@ func TestExclude(t *testing.T) {
t.Fatal("Exclude(MGT.*):", err)
return
}
if err := s.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
return
}
names, err := s.Names()
......@@ -423,8 +414,8 @@ func TestExclude(t *testing.T) {
t.Fatal("Exclude(MGT.uptime):", err)
return
}
if err := s2.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s2.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
return
}
names, err = s2.Names()
......@@ -450,8 +441,8 @@ func TestExclude(t *testing.T) {
}
attached := New()
defer attached.Release()
if err := attached.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := attached.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
if err := attached.Exclude(fld); err == nil {
t.Error("expected Exclude() to fail for attached instance")
......@@ -477,8 +468,8 @@ func TestInExclude(t *testing.T) {
t.Fatal("Exclude(MGT.*):", err)
return
}
if err := s.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
return
}
......@@ -517,8 +508,8 @@ func TestRead(t *testing.T) {
s := New()
defer s.Release()
if err := s.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
if err := s.Read(nil); err == nil {
t.Error("expected Read(nil) to fail")
......@@ -528,8 +519,8 @@ func TestRead(t *testing.T) {
func TestE2EDefaultRead(t *testing.T) {
s := New()
defer s.Release()
if err := s.Attach(); err != nil {
t.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
t.Fatal("Attach(default):", err)
}
var stats = make(map[string]uint64)
......@@ -602,8 +593,8 @@ func TestE2EDefaultRead(t *testing.T) {
func BenchmarkReadOneStat(b *testing.B) {
s := New()
defer s.Release()
if err := s.Attach(); err != nil {
b.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
b.Fatal("Attach(default):", err)
return
}
......@@ -630,8 +621,8 @@ func BenchmarkReadOneStat(b *testing.B) {
func BenchmarkReadAllStats(b *testing.B) {
s := New()
defer s.Release()
if err := s.Attach(); err != nil {
b.Fatal("Attach():", err)
if err := s.Attach(""); err != nil {
b.Fatal("Attach(default):", err)
return
}
......
......@@ -422,28 +422,17 @@ func (stats *Stats) getD9ns() error {
return nil
}
// Attach to the default instance of Varnish -- the instance of
// varnishd invoked without the -n option.
func (stats *Stats) Attach() error {
if err := stats.initVSM(); err != nil {
return err
}
if err := stats.vsm.Attach(false); err != nil {
return err
}
if err := stats.getD9ns(); err != nil {
return err
}
return nil
}
// AttachInstance attaches to a named instance of Varnish, where name
// corresponds to the -n argument in the invocation of varnishd.
func (stats *Stats) AttachInstance(name string) error {
// Attach to an instance of Varnish -- varnishd with a running worker
// process.
//
// If name != "", then attach to the default instance -- varnishd
// invoked without the -n option. Otherwise, attach to the named
// instance, as given in the -n option.
func (stats *Stats) Attach(name string) error {
if err := stats.initVSM(); err != nil {
return err
}
if err := stats.vsm.AttachInstance(name, false); err != nil {
if err := stats.vsm.Attach(name); err != nil {
return err
}
if err := stats.getD9ns(); err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment