oui_test: fix data race when overriding ouiURL

This commit is contained in:
Michael Stapelberg 2019-01-06 18:07:59 +01:00
parent 6320b6c3a7
commit 13926217d9
2 changed files with 16 additions and 9 deletions

View File

@ -32,15 +32,26 @@ type DB struct {
orgs map[string]string orgs map[string]string
} }
type option func(d *DB)
func ouiURL(u string) option {
return func(d *DB) {
d.ouiURL = u
}
}
// NewDB loads a database from the cached version in dir, if any, and // NewDB loads a database from the cached version in dir, if any, and
// asynchronously triggers an update. Use WaitUntilLoaded() to ensure Lookup() // asynchronously triggers an update. Use WaitUntilLoaded() to ensure Lookup()
// will work, or use Lookup() opportunistically at any time. // will work, or use Lookup() opportunistically at any time.
func NewDB(dir string) *DB { func NewDB(dir string, opts ...option) *DB {
db := &DB{ db := &DB{
dir: dir, dir: dir,
ouiURL: "http://standards-oui.ieee.org/oui/oui.csv", ouiURL: "http://standards-oui.ieee.org/oui/oui.csv",
} }
db.cond = sync.NewCond(&db.Mutex) db.cond = sync.NewCond(&db.Mutex)
for _, o := range opts {
o(db)
}
go db.update() go db.update()
return db return db
} }

View File

@ -33,8 +33,7 @@ MA-L,4409B8,"Salcomp (Shenzhen) CO., LTD.","Salcomp Road, Furong Industrial Area
})) }))
defer srv.Close() defer srv.Close()
db := NewDB(tmpdir) db := NewDB(tmpdir, ouiURL(srv.URL))
db.ouiURL = srv.URL
if err := db.WaitUntilLoaded(); err != nil { if err := db.WaitUntilLoaded(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -56,8 +55,7 @@ MA-L,4409B8,"Salcomp (Shenzhen) CO., LTD.","Salcomp Road, Furong Industrial Area
})) }))
defer srv.Close() defer srv.Close()
db := NewDB(tmpdir) db := NewDB(tmpdir, ouiURL(srv.URL))
db.ouiURL = srv.URL
if err := db.WaitUntilLoaded(); err != nil { if err := db.WaitUntilLoaded(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -87,8 +85,7 @@ MA-L,4409B8,"Salcomp (Shenzhen) CO., LTD.","Salcomp Road, Furong Industrial Area
})) }))
defer srv.Close() defer srv.Close()
db := NewDB(tmpdir) db := NewDB(tmpdir, ouiURL(srv.URL))
db.ouiURL = srv.URL
if err := db.WaitUntilLoaded(); err != nil { if err := db.WaitUntilLoaded(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -121,8 +118,7 @@ MA-L,F09FC2,Obiquiti Networks Inc.,2580 Orchard Parkway San Jose CA US 95131
})) }))
defer srv.Close() defer srv.Close()
db := NewDB(tmpdir) db := NewDB(tmpdir, ouiURL(srv.URL))
db.ouiURL = srv.URL
if err := db.WaitUntilLoaded(); err != nil { if err := db.WaitUntilLoaded(); err != nil {
t.Fatal(err) t.Fatal(err)
} }