MM-22056: Fix flaky test TestUploadFiles (#14234)

Automatic Merge
Этот коммит содержится в:
Agniva De Sarker
2020-04-07 19:39:04 +05:30
коммит произвёл GitHub
родитель 22a00104bd
Коммит 4210ae60ab
5 изменённых файлов: 19 добавлений и 50 удалений

Просмотреть файл

@@ -5,11 +5,10 @@ package api4
import (
"bytes"
"encoding/hex"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"math/rand"
"mime/multipart"
"net/http"
"net/textproto"
@@ -222,13 +221,13 @@ func TestUploadFiles(t *testing.T) {
// Upload a bunch of files, mixed images and non-images
{
title: "Happy",
names: []string{"test.png", "testgif.gif", "testplugin.tar.gz", "test-search.md", "test_compressed_tiff.tiff"},
names: []string{"test.png", "testgif.gif", "testplugin.tar.gz", "test-search.md", "test.tiff"},
expectedCreatorId: th.BasicUser.Id,
},
// Upload a bunch of files, with clientIds
{
title: "Happy client_ids",
names: []string{"test.png", "testgif.gif", "testplugin.tar.gz", "test-search.md", "test_compressed_tiff.tiff"},
names: []string{"test.png", "testgif.gif", "testplugin.tar.gz", "test-search.md", "test.tiff"},
clientIds: []string{"1", "2", "3", "4", "5"},
expectedCreatorId: th.BasicUser.Id,
},
@@ -357,18 +356,7 @@ func TestUploadFiles(t *testing.T) {
// TIFF preview test
{
title: "Happy image thumbnail/preview 9",
names: []string{"test_compressed_tiff.tiff"},
expectedImageThumbnailNames: []string{"test_expected_tiff_thumb.jpeg"},
expectedImagePreviewNames: []string{"test_expected_tiff_preview.jpeg"},
expectImage: true,
expectedImageWidths: []int{701},
expectedImageHeights: []int{701},
expectedImageHasPreview: []bool{true},
expectedCreatorId: th.BasicUser.Id,
},
{
title: "Happy image thumbnail/preview 10",
names: []string{"test_raw_tiff.tiff"},
names: []string{"test.tiff"},
expectedImageThumbnailNames: []string{"test_expected_tiff_thumb.jpeg"},
expectedImagePreviewNames: []string{"test_expected_tiff_preview.jpeg"},
expectImage: true,
@@ -625,12 +613,7 @@ func TestUploadFiles(t *testing.T) {
require.Nil(t, err)
_, err = io.Copy(tf, bytes.NewReader(data))
require.Nil(t, err)
if strings.Contains(name, "test_expected_tiff") {
// TODO: remove this once MM-22056 is fixed.
t.Errorf("Actual data mismatched %s, written to %q - expected %d bytes, got %d. Previewer Image: \n\n%s\n", name, tf.Name(), len(expected), len(data), hex.Dump(data))
} else {
t.Errorf("Actual data mismatched %s, written to %q - expected %d bytes, got %d.", name, tf.Name(), len(expected), len(data))
}
t.Errorf("Actual data mismatched %s, written to %q - expected %d bytes, got %d.", name, tf.Name(), len(expected), len(data))
}
}
if len(tc.expectedPayloadNames) == 0 {
@@ -767,9 +750,6 @@ func TestGetFileThumbnail(t *testing.T) {
fileId := fileResp.FileInfos[0].Id
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
data, resp := Client.GetFileThumbnail(fileId)
CheckNoError(t, resp)
require.NotEqual(t, 0, len(data), "should not be empty")
@@ -826,9 +806,6 @@ func TestGetFileLink(t *testing.T) {
_, resp = Client.GetFileLink(fileId)
CheckNotImplementedStatus(t, resp)
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true })
link, resp := Client.GetFileLink(fileId)
CheckNoError(t, resp)
@@ -875,9 +852,6 @@ func TestGetFilePreview(t *testing.T) {
CheckNoError(t, resp)
fileId := fileResp.FileInfos[0].Id
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
data, resp := Client.GetFilePreview(fileId)
CheckNoError(t, resp)
require.NotEqual(t, 0, len(data), "should not be empty")
@@ -920,9 +894,6 @@ func TestGetFileInfo(t *testing.T) {
CheckNoError(t, resp)
fileId := fileResp.FileInfos[0].Id
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
info, resp := Client.GetFileInfo(fileId)
CheckNoError(t, resp)
@@ -980,9 +951,6 @@ func TestGetPublicFile(t *testing.T) {
require.Nil(t, err)
link := th.App.GeneratePublicLink(Client.Url, info)
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
resp, err := http.Get(link)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, "failed to get image with public link")

Просмотреть файл

@@ -26,6 +26,7 @@ import (
"github.com/disintegration/imaging"
"github.com/rwcarlsen/goexif/exif"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
@@ -796,29 +797,29 @@ func (t *UploadFileTask) postprocessImage() {
return
}
const jpegQuality = 90
writeJPEG := func(img image.Image, path string) {
r, w := io.Pipe()
go func() {
_, aerr := t.writeFile(r, path)
if aerr != nil {
mlog.Error("Unable to upload", mlog.String("path", path), mlog.Err(aerr))
return
err := jpeg.Encode(w, img, &jpeg.Options{Quality: jpegQuality})
if err != nil {
mlog.Error("Unable to encode image as jpeg", mlog.String("path", path), mlog.Err(err))
w.CloseWithError(err)
} else {
w.Close()
}
}()
err := jpeg.Encode(w, img, &jpeg.Options{Quality: 90})
if err != nil {
mlog.Error("Unable to encode image as jpeg", mlog.String("path", path), mlog.Err(err))
w.CloseWithError(err)
} else {
w.Close()
_, aerr := t.writeFile(r, path)
if aerr != nil {
mlog.Error("Unable to upload", mlog.String("path", path), mlog.Err(aerr))
return
}
}
w := decoded.Bounds().Dx()
h := decoded.Bounds().Dy()
wg := &sync.WaitGroup{}
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()

Просмотреть файл

@@ -2181,7 +2181,7 @@ func TestParseImages(t *testing.T) {
},
},
"tiff": {
FileName: "test_compressed_tiff.tiff",
FileName: "test.tiff",
Expected: (*model.PostImage)(nil),
},
"not an image": {

Просмотреть файл

Двоичные данные
tests/test_raw_tiff.tiff

Двоичный файл не отображается.