From 477f2fc5ea2bdf608fe7e9215191c6b8334f4200 Mon Sep 17 00:00:00 2001 From: saurabh-narkhede Date: Mon, 25 Jul 2022 17:02:51 +0530 Subject: [PATCH] UOE-7910 Added Fetcher Module --- fetcher.go | 36 +++++++++++--------- fetcher_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 fetcher_test.go diff --git a/fetcher.go b/fetcher.go index 4d4b06f..44812bd 100644 --- a/fetcher.go +++ b/fetcher.go @@ -1,23 +1,29 @@ -package go-cache +package cache + +import ( + "errors" + "fmt" +) const ( errInvalidKey = "type:[invalid_key] key:[%s]" ) -type callbackFunc func(key string)(interface{},error) + +type callbackFunc func(key string) (interface{}, error) type fetcher struct { - cb map[string]callbackFunc + cb map[string]callbackFunc prefixLen int } func NewFetcher(prefixLen int) *fetcher { return &fetcher{ - cb : make(map[string]callbackFunc), + cb: make(map[string]callbackFunc), prefixLen: prefixLen, } } -func (f *fetcher) Register(keyPrefix string, cbf callbackFunc) bool{ +func (f *fetcher) Register(keyPrefix string, cbf callbackFunc) bool { if len(keyPrefix) != f.prefixLen { return false } @@ -25,31 +31,30 @@ func (f *fetcher) Register(keyPrefix string, cbf callbackFunc) bool{ return true } -func (f *fetcher) execute(key string) (interface{},error) { +func (f *fetcher) Execute(key string) (interface{}, error) { if len(key) < f.prefixLen { - return nil, fmt.Errorf(errInvalidKey,key) + return nil, errors.New("Invalid Key " + key) } keyPrefix := key[:f.prefixLen] cbf, ok := f.cb[keyPrefix] if !ok { - return nil, fmt.Errorf(errInvalidKey,key) + return nil, fmt.Errorf(errInvalidKey, key) } return cbf(key) } - /* //header bidding sample keys -Key_Prefix_PUB_SLOT_INFO = "AAA00" +Key_Prefix_PUB_SLOT_INFO = "AAA00" -> DB Key_Prefix_PUB_HB_PARTNER = "AAB00" Key_Prefix_PubAdunitConfig = "AAC00" Key_Prefix_PubSlotHashInfo = "AAD00" Key_Prefix_PubSlotRegex = "AAE00" Key_Prefix_PubSlotNameHash = "AAF00" -Key_Prefix_PubVASTTags = "AAG00" +Key_Prefix_PubVASTTags = "AAG00" -> DBPubVasttags(key string) (interface,error) PUB_SLOT_INFO = Key_Prefix_PUB_SLOT_INFO + "_%d_%d_%d_%d" // publisher slot mapping at publisher, profile, display version and adapter level @@ -64,6 +69,7 @@ func DBGetVASTTags(key string) (interface{},error) { strings.Split(key,"_") key[0] // keyprefix key[1] //publisherid + return GetVastTag(rtb req, pub ID, comn etc) } //hb @@ -75,10 +81,10 @@ type AsyncCache struct { ks *keystatus } -func (c *AsycCache) aget(key string) { - data, err := c.f.execute(key) +func (ac *AsyncCache) aget(key string) { + data, err := ac.f.execute(key) if err != nil { - c.c.set(key, data) + ac.c.set(key, data) } } -*/ \ No newline at end of file +*/ diff --git a/fetcher_test.go b/fetcher_test.go new file mode 100644 index 0000000..55af0bd --- /dev/null +++ b/fetcher_test.go @@ -0,0 +1,90 @@ +package cache + +import ( + "reflect" + "testing" +) + +func Test_fetcher_Execute(t *testing.T) { + type fields struct { + cb map[string]callbackFunc + prefixLen int + } + type args struct { + key string + } + tests := []struct { + name string + fields fields + args args + want interface{} + wantErr bool + }{ + // TODO: Add test cases. + { + name: "Invalid Key", + fields: fields{ + cb: map[string]callbackFunc{ + "AAG00": nil, + }, + prefixLen: 5, + }, + args: args{ + key: "INV", + }, + want: nil, + wantErr: true, + }, + { + name: "Unexisting Key Execution", + fields: fields{ + cb: map[string]callbackFunc{ + "AAG00": nil, + "AAA00": nil, + "AAB00": nil, + }, + prefixLen: 5, + }, + args: args{ + key: "UnExisted_Key", + }, + want: nil, + wantErr: true, + }, + { + name: "Valid Key Execution", + fields: fields{ + cb: make(map[string]callbackFunc), + prefixLen: 5, + }, + args: args{ + key: "AAG00_5890", + }, + want: f.cb{ + "afewv", + }, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := &fetcher{ + cb: tt.fields.cb, + prefixLen: tt.fields.prefixLen, + } + got, err := f.Execute(tt.args.key) + // if errors.Is(err, tt.wantErr) { + // t.Errorf("fetcher.Execute() error = %v, wantErr %v", err, tt.wantErr) + // return + // } + if (err != nil) != tt.wantErr { + t.Errorf("fetcher.Execute() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("fetcher.Execute() = %v, want %v", got, tt.want) + } + }) + } +}