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