summaryrefslogtreecommitdiff
path: root/internal/processing/admin/workertask_test.go
blob: bf326bafd9ef8991993bcf548dace19355ffea0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package admin_test

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"testing"

	"github.com/stretchr/testify/suite"
	"github.com/superseriousbusiness/gotosocial/internal/ap"
	"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/httpclient"
	"github.com/superseriousbusiness/gotosocial/internal/messages"
	"github.com/superseriousbusiness/gotosocial/internal/transport/delivery"
	"github.com/superseriousbusiness/gotosocial/testrig"
)

var (
	// TODO: move these test values into
	// the testrig test models area. They'll
	// need to be as both WorkerTask and as
	// the raw types themselves.

	testDeliveries = []*delivery.Delivery{
		{
			ObjectID: "https://google.com/users/bigboy/follow/1",
			TargetID: "https://askjeeves.com/users/smallboy",
			Request:  toRequest("POST", "https://askjeeves.com/users/smallboy/inbox", []byte("data!"), http.Header{"Host": {"https://askjeeves.com"}}),
		},
		{
			Request: toRequest("GET", "https://google.com", []byte("uwu im just a wittle seawch engwin"), http.Header{"Host": {"https://google.com"}}),
		},
	}

	testFederatorMsgs = []*messages.FromFediAPI{
		{
			APObjectType:   ap.ObjectNote,
			APActivityType: ap.ActivityCreate,
			TargetURI:      "https://gotosocial.org",
			Requesting:     &gtsmodel.Account{ID: "654321"},
			Receiving:      &gtsmodel.Account{ID: "123456"},
		},
		{
			APObjectType:   ap.ObjectProfile,
			APActivityType: ap.ActivityUpdate,
			TargetURI:      "https://uk-queen-is-dead.org",
			Requesting:     &gtsmodel.Account{ID: "123456"},
			Receiving:      &gtsmodel.Account{ID: "654321"},
		},
	}

	testClientMsgs = []*messages.FromClientAPI{
		{
			APObjectType:   ap.ObjectNote,
			APActivityType: ap.ActivityCreate,
			TargetURI:      "https://gotosocial.org",
			Origin:         &gtsmodel.Account{ID: "654321"},
			Target:         &gtsmodel.Account{ID: "123456"},
		},
		{
			APObjectType:   ap.ObjectProfile,
			APActivityType: ap.ActivityUpdate,
			TargetURI:      "https://uk-queen-is-dead.org",
			Origin:         &gtsmodel.Account{ID: "123456"},
			Target:         &gtsmodel.Account{ID: "654321"},
		},
	}
)

type WorkerTaskTestSuite struct {
	AdminStandardTestSuite
}

func (suite *WorkerTaskTestSuite) TestFillWorkerQueues() {
	ctx, cncl := context.WithCancel(context.Background())
	defer cncl()

	var tasks []*gtsmodel.WorkerTask

	for _, dlv := range testDeliveries {
		// Serialize all test deliveries.
		data, err := dlv.Serialize()
		if err != nil {
			panic(err)
		}

		// Append each serialized delivery to tasks.
		tasks = append(tasks, &gtsmodel.WorkerTask{
			WorkerType: gtsmodel.DeliveryWorker,
			TaskData:   data,
		})
	}

	for _, msg := range testFederatorMsgs {
		// Serialize all test messages.
		data, err := msg.Serialize()
		if err != nil {
			panic(err)
		}

		if msg.Receiving != nil {
			// Quick hack to bypass database errors for non-existing
			// accounts, instead we just insert this into cache ;).
			suite.state.Caches.DB.Account.Put(msg.Receiving)
			suite.state.Caches.DB.AccountSettings.Put(&gtsmodel.AccountSettings{
				AccountID: msg.Receiving.ID,
			})
		}

		if msg.Requesting != nil {
			// Quick hack to bypass database errors for non-existing
			// accounts, instead we just insert this into cache ;).
			suite.state.Caches.DB.Account.Put(msg.Requesting)
			suite.state.Caches.DB.AccountSettings.Put(&gtsmodel.AccountSettings{
				AccountID: msg.Requesting.ID,
			})
		}

		// Append each serialized message to tasks.
		tasks = append(tasks, &gtsmodel.WorkerTask{
			WorkerType: gtsmodel.FederatorWorker,
			TaskData:   data,
		})
	}

	for _, msg := range testClientMsgs {
		// Serialize all test messages.
		data, err := msg.Serialize()
		if err != nil {
			panic(err)
		}

		if msg.Origin != nil {
			// Quick hack to bypass database errors for non-existing
			// accounts, instead we just insert this into cache ;).
			suite.state.Caches.DB.Account.Put(msg.Origin)
			suite.state.Caches.DB.AccountSettings.Put(&gtsmodel.AccountSettings{
				AccountID: msg.Origin.ID,
			})
		}

		if msg.Target != nil {
			// Quick hack to bypass database errors for non-existing
			// accounts, instead we just insert this into cache ;).
			suite.state.Caches.DB.Account.Put(msg.Target)
			suite.state.Caches.DB.AccountSettings.Put(&gtsmodel.AccountSettings{
				AccountID: msg.Target.ID,
			})
		}

		// Append each serialized message to tasks.
		tasks = append(tasks, &gtsmodel.WorkerTask{
			WorkerType: gtsmodel.ClientWorker,
			TaskData:   data,
		})
	}

	// Persist all test worker tasks to the database.
	err := suite.state.DB.PutWorkerTasks(ctx, tasks)
	suite.NoError(err)

	// Fill the worker queues from persisted task data.
	err = suite.adminProcessor.FillWorkerQueues(ctx)
	suite.NoError(err)

	var (
		// Recovered
		// task counts.
		ndelivery  int
		nfederator int
		nclient    int
	)

	// Fetch current gotosocial instance account, for later checks.
	instanceAcc, err := suite.state.DB.GetInstanceAccount(ctx, "")
	suite.NoError(err)

	for {
		// Pop all queued delivery tasks from worker queue.
		dlv, ok := suite.state.Workers.Delivery.Queue.Pop()
		if !ok {
			break
		}

		// Incr count.
		ndelivery++

		// Check that we have this message in slice.
		err = containsSerializable(testDeliveries, dlv)
		suite.NoError(err)

		// Check that delivery request context has instance account pubkey.
		pubKeyID := gtscontext.OutgoingPublicKeyID(dlv.Request.Context())
		suite.Equal(instanceAcc.PublicKeyURI, pubKeyID)
		signfn := gtscontext.HTTPClientSignFunc(dlv.Request.Context())
		suite.NotNil(signfn)
	}

	for {
		// Pop all queued federator messages from worker queue.
		msg, ok := suite.state.Workers.Federator.Queue.Pop()
		if !ok {
			break
		}

		// Incr count.
		nfederator++

		// Check that we have this message in slice.
		err = containsSerializable(testFederatorMsgs, msg)
		suite.NoError(err)
	}

	for {
		// Pop all queued client messages from worker queue.
		msg, ok := suite.state.Workers.Client.Queue.Pop()
		if !ok {
			break
		}

		// Incr count.
		nclient++

		// Check that we have this message in slice.
		err = containsSerializable(testClientMsgs, msg)
		suite.NoError(err)
	}

	// Ensure recovered task counts as expected.
	suite.Equal(len(testDeliveries), ndelivery)
	suite.Equal(len(testFederatorMsgs), nfederator)
	suite.Equal(len(testClientMsgs), nclient)
}

func (suite *WorkerTaskTestSuite) TestPersistWorkerQueues() {
	ctx, cncl := context.WithCancel(context.Background())
	defer cncl()

	// Push all test worker tasks to their respective queues.
	suite.state.Workers.Delivery.Queue.Push(testDeliveries...)
	suite.state.Workers.Federator.Queue.Push(testFederatorMsgs...)
	suite.state.Workers.Client.Queue.Push(testClientMsgs...)

	// Persist the worker queued tasks to database.
	err := suite.adminProcessor.PersistWorkerQueues(ctx)
	suite.NoError(err)

	// Fetch all the persisted tasks from database.
	tasks, err := suite.state.DB.GetWorkerTasks(ctx)
	suite.NoError(err)

	var (
		// Persisted
		// task counts.
		ndelivery  int
		nfederator int
		nclient    int
	)

	// Check persisted task data.
	for _, task := range tasks {
		switch task.WorkerType {
		case gtsmodel.DeliveryWorker:
			var dlv delivery.Delivery

			// Incr count.
			ndelivery++

			// Deserialize the persisted task data.
			err := dlv.Deserialize(task.TaskData)
			suite.NoError(err)

			// Check that we have this delivery in slice.
			err = containsSerializable(testDeliveries, &dlv)
			suite.NoError(err)

		case gtsmodel.FederatorWorker:
			var msg messages.FromFediAPI

			// Incr count.
			nfederator++

			// Deserialize the persisted task data.
			err := msg.Deserialize(task.TaskData)
			suite.NoError(err)

			// Check that we have this message in slice.
			err = containsSerializable(testFederatorMsgs, &msg)
			suite.NoError(err)

		case gtsmodel.ClientWorker:
			var msg messages.FromClientAPI

			// Incr count.
			nclient++

			// Deserialize the persisted task data.
			err := msg.Deserialize(task.TaskData)
			suite.NoError(err)

			// Check that we have this message in slice.
			err = containsSerializable(testClientMsgs, &msg)
			suite.NoError(err)

		default:
			suite.T().Errorf("unexpected worker type: %d", task.WorkerType)
		}
	}

	// Ensure persisted task counts as expected.
	suite.Equal(len(testDeliveries), ndelivery)
	suite.Equal(len(testFederatorMsgs), nfederator)
	suite.Equal(len(testClientMsgs), nclient)
}

func (suite *WorkerTaskTestSuite) SetupTest() {
	suite.AdminStandardTestSuite.SetupTest()
	// we don't want workers running
	testrig.StopWorkers(&suite.state)
}

func TestWorkerTaskTestSuite(t *testing.T) {
	suite.Run(t, new(WorkerTaskTestSuite))
}

// containsSerializeable returns whether slice of serializables contains given serializable entry.
func containsSerializable[T interface{ Serialize() ([]byte, error) }](expect []T, have T) error {
	// Serialize wanted value.
	bh, err := have.Serialize()
	if err != nil {
		panic(err)
	}

	var strings []string

	for _, t := range expect {
		// Serialize expected value.
		be, err := t.Serialize()
		if err != nil {
			panic(err)
		}

		// Alloc as string.
		se := string(be)

		if se == string(bh) {
			// We have this entry!
			return nil
		}

		// Add to serialized strings.
		strings = append(strings, se)
	}

	return fmt.Errorf("could not find %s in %s", string(bh), strings)
}

// urlStr simply returns u.String() or "" if nil.
func urlStr(u *url.URL) string {
	if u == nil {
		return ""
	}
	return u.String()
}

// accountID simply returns account.ID or "" if nil.
func accountID(account *gtsmodel.Account) string {
	if account == nil {
		return ""
	}
	return account.ID
}

// toRequest creates httpclient.Request from HTTP method, URL and body data.
func toRequest(method string, url string, body []byte, hdr http.Header) *httpclient.Request {
	var rbody io.Reader
	if body != nil {
		rbody = bytes.NewReader(body)
	}
	req, err := http.NewRequest(method, url, rbody)
	if err != nil {
		panic(err)
	}
	for key, values := range hdr {
		for _, value := range values {
			req.Header.Add(key, value)
		}
	}
	return httpclient.WrapRequest(req)
}

// toJSON marshals input type as JSON data.
func toJSON(a any) []byte {
	b, err := json.Marshal(a)
	if err != nil {
		panic(err)
	}
	return b
}