Skip to content

Commit

Permalink
fix: cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
airenas committed Nov 8, 2024
1 parent fc5d18e commit 6467d56
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
5 changes: 3 additions & 2 deletions internal/pkg/cache/bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"fmt"
"log"
"strconv"
"time"

"github.com/airenas/go-app/pkg/goapp"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *BigCacher) Work(inp *api.TTSRequestConfig) (*api.Result, error) {
}

func (c *BigCacher) isOK(inp *api.TTSRequestConfig) bool {
return (c.maxTextLen == 0 || len(inp.Text) <= c.maxTextLen) && inp.OutputTextFormat == api.TextNone
return (c.maxTextLen == 0 || len(inp.Text) <= c.maxTextLen) && inp.OutputTextFormat == api.TextNone && len(inp.SpeechMarkTypes) == 0
}

func getCleanDuration(dur time.Duration) time.Duration {
Expand All @@ -86,5 +87,5 @@ func getCleanDuration(dur time.Duration) time.Duration {
}

func key(inp *api.TTSRequestConfig) string {
return inp.Text + "_" + inp.OutputFormat.String() + "_" + fmt.Sprintf("%.4f", inp.Speed) + "_" + inp.Voice
return inp.Text + "_" + inp.OutputFormat.String() + "_" + fmt.Sprintf("%.4f", inp.Speed) + "_" + inp.Voice + "_" + strconv.FormatInt(inp.MaxEdgeSilenceMillis, 10)
}
85 changes: 55 additions & 30 deletions internal/pkg/cache/bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"testing"
"time"

"github.com/airenas/tts-line/internal/pkg/service/api"
"github.com/airenas/tts-line/internal/pkg/test/mocks"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/airenas/tts-line/internal/pkg/service/api"
"github.com/airenas/tts-line/internal/pkg/test/mocks"
)

var (
Expand Down Expand Up @@ -112,17 +111,6 @@ func TestWork_Key(t *testing.T) {
synthesizerMock.AssertNumberOfCalls(t, "Work", 3)
}

func Test_Key(t *testing.T) {
initTest(t)
assert.Equal(t, "olia_mp3_0.0000_", key(&api.TTSRequestConfig{Text: "olia", OutputFormat: api.AudioMP3}))
assert.Equal(t, "olia1_m4a_0.0000_aaa", key(&api.TTSRequestConfig{Text: "olia1", OutputFormat: api.AudioM4A,
OutputTextFormat: api.TextAccented, Voice: "aaa"}))
assert.Equal(t, "olia1_m4a_0.5600_aa", key(&api.TTSRequestConfig{Text: "olia1", OutputFormat: api.AudioM4A,
OutputTextFormat: api.TextAccented, Speed: 0.56, Voice: "aa"}))
assert.Equal(t, "olia1_m4a_0.5600_aaa", key(&api.TTSRequestConfig{Text: "olia1", OutputFormat: api.AudioM4A,
OutputTextFormat: api.TextAccented, Speed: 0.56, Voice: "aaa"}))
}

func Test_MaxMB(t *testing.T) {
initTest(t)
c, _ := NewCacher(synthesizerMock, newTestConfig("duration: 10s\nmaxMB: 1"))
Expand Down Expand Up @@ -154,22 +142,6 @@ func Test_MaxTextLen(t *testing.T) {
synthesizerMock.AssertNumberOfCalls(t, "Work", 3)
}

func TestIsOK(t *testing.T) {
initTest(t)
c, _ := NewCacher(synthesizerMock, newTestConfig("duration: 10s\nmaxTextLen: 10"))
d := &api.TTSRequestConfig{}
d.Text = "aaa"
d.OutputTextFormat = api.TextNone
assert.True(t, c.isOK(d))
d.OutputTextFormat = api.TextAccented
assert.False(t, c.isOK(d))
d.OutputTextFormat = api.TextNormalized
assert.False(t, c.isOK(d))
d.OutputTextFormat = api.TextNone
d.Text = "111111111111111"
assert.False(t, c.isOK(d))
}

func newTestConfig(yaml string) *viper.Viper {
res := viper.New()
res.SetConfigType("yaml")
Expand All @@ -184,3 +156,56 @@ func newtestInput(txt string) *api.TTSRequestConfig {
func strOfSize(s int) string {
return string(make([]byte, s))
}

func TestBigCacher_isOK(t *testing.T) {
c, _ := NewCacher(synthesizerMock, newTestConfig("duration: 10s\nmaxTextLen: 10"))
type args struct {
inp *api.TTSRequestConfig
}
tests := []struct {
name string
args args
want bool
}{
{"OK", args{&api.TTSRequestConfig{Text: "aaa", OutputTextFormat: api.TextNone}}, true},
{"Accented", args{&api.TTSRequestConfig{Text: "aaa", OutputTextFormat: api.TextAccented}}, false},
{"Normalized", args{&api.TTSRequestConfig{Text: "aaa", OutputTextFormat: api.TextNormalized}}, false},
{"Long", args{&api.TTSRequestConfig{Text: "111111111111111", OutputTextFormat: api.TextNone}}, false},
{"tags", args{&api.TTSRequestConfig{Text: "aaa", OutputTextFormat: api.TextNone, SpeechMarkTypes: map[string]bool{"word": true}}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := c.isOK(tt.args.inp); got != tt.want {
t.Errorf("BigCacher.isOK() = %v, want %v", got, tt.want)
}
})
}
}

func Test_key(t *testing.T) {
type args struct {
inp *api.TTSRequestConfig
}
tests := []struct {
name string
args args
want string
}{
{"mp3", args{&api.TTSRequestConfig{Text: "olia", OutputFormat: api.AudioMP3}}, "olia_mp3_0.0000__0"},
{"voice", args{&api.TTSRequestConfig{Text: "olia1", OutputFormat: api.AudioM4A,
OutputTextFormat: api.TextAccented, Voice: "aaa"}}, "olia1_m4a_0.0000_aaa_0"},
{"speed", args{&api.TTSRequestConfig{Text: "olia1", OutputFormat: api.AudioM4A,
OutputTextFormat: api.TextAccented, Speed: 0.56, Voice: "aa"}}, "olia1_m4a_0.5600_aa_0"},
{"test 2", args{&api.TTSRequestConfig{Text: "olia1", OutputFormat: api.AudioM4A,
OutputTextFormat: api.TextAccented, Speed: 0.56, Voice: "aaa"}}, "olia1_m4a_0.5600_aaa_0"},
{"max sil duration", args{&api.TTSRequestConfig{Text: "olia1", OutputFormat: api.AudioM4A,
OutputTextFormat: api.TextAccented, Speed: 0.56, Voice: "aaa", MaxEdgeSilenceMillis: 50}}, "olia1_m4a_0.5600_aaa_50"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := key(tt.args.inp); got != tt.want {
t.Errorf("key() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 6467d56

Please sign in to comment.